Literal types? #1166
Replies: 4 comments 9 replies
-
So, very technically, no -- F# doesn't have this feature the way some other languages do. However, given your very limited examples, I don't see anything that cannot be accomplished with a garden-variety Discriminated Union. 🤷♂️ But presumably, a more sophisticated example would reveal the limitations/trade-offs of not having this feature. |
Beta Was this translation helpful? Give feedback.
-
I'm not entirely sure whether you mean "literal" as in a compile-time literal that's like a constant type that restricts the types of a constant (i.e. with While not a perfect equivalent, literals can be expressed as
Or even like this, which, granted, nobody should write:
I kinda like the idea of limited string literals as a type, but I don't know if it adds enough value to the already available normal literals. I mean, if this is compile-time only, you could just as well create a few literals in a module that you'd call |
Beta Was this translation helpful? Give feedback.
-
This has been briefly discussed in this issue: #656 I'd be curious what @dsyme's take is now that some preliminary work has landed that could support a feature like this in the future. |
Beta Was this translation helpful? Give feedback.
-
This is now back in scope, see #1195 |
Beta Was this translation helpful? Give feedback.
-
I've learning F# for some days and coming from Typescript and Scala context i miss declaring a literal types ( types defined by numeric and/or string values )
Literal types in typescript, source:
Scala also supports literal types, source
Rescript which is somehow related to F# because of its OCaml influence has Polymorphic Variant, which allow to use strings and numbers literals as types by using a intentional different syntax.
Could that be possible on F#, using literals as types?
Beta Was this translation helpful? Give feedback.
All reactions