I suggest we ...

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

36 votes
Vote
Sign in
(thinking…)
Password icon
Signed in as (Sign out)
You have left! (?) (thinking…)
Gustavo Guerra shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

10 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • Anonymous commented  ·   ·  Flag as inappropriate

    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.

  • Gustavo Guerra commented  ·   ·  Flag as inappropriate

    Type aliases only would help a bit but not really solve the problem completely. Having local records and DUs would be much better

  • Gustavo Guerra commented  ·   ·  Flag as inappropriate

    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

  • Gustavo Guerra commented  ·   ·  Flag as inappropriate

    (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  ·   ·  Flag as inappropriate

    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  ·   ·  Flag as inappropriate

    At a minimum type aliasing at local scopes as per C++ typedef would be really handy. Nested type declaration would also be nice.

F# Language

Feedback and Knowledge Base