Allow to define types at expression scopes
The suggestion is to allow "type X = ..." in an expression, e.g.
let f () =
type X = A | B
let x = (A,A)
let y = (B,B)
if (x=y) then true else false
Daniel Little commented
It would be quite helpful to define a type that's only used in a single function
I was surprised to find this isn't inherently supported, when I first coded it it seemed quite natural because I can declare an inner function within a parent function and the inner function is known only within that parent, I added a simple enum type - also within some parent function and the compiler complained.
Type aliases only would help a bit but not really solve the problem completely. Having local records and DUs would be much better
This was also added to TypeScript: https://www.typescriptlang.org/docs/release-notes/typescript-1.6.html
Don Syme commented
What if only type aliases were allowed?
Vasily Kirichenko commented
This is now it's implemented in D https://gist.github.com/vasily-kirichenko/95a2cabfba599884b61d
So, no-static types can capture local variables and cannot escape the function scope.
Actually, even if members weren't allowed this would be already very useful. On local scope we usually use a lot of tuples, and refactoring to records or DUs is usefull, but it's a bit annoying that you have to do that at global scope
(b) Should not be possible, as these types should be private to the function, so an error should be issued on that case
(a) Yes, they should be full type definitions. Capturing values from scope would be an added bonus, but would be also ok if that wasn't supported, I guess.
Don Syme commented
The technical questions that arise are
(a) can these include full type definitions - including members? If so, can these capture values from scope?
(b) can these types appear in the inferred type of the function?
Phillip Trelford commented
At a minimum type aliasing at local scopes as per C++ typedef would be really handy. Nested type declaration would also be nice.