-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Literals as types #1195
Comments
Looks like it is too early in the morning and my ability to do a search properly is still limited before the first coffee. RFC FS-1092 indeed looks exactly like it. |
The suggestion here is misnamed, it's actually for typescript style literals-as-types |
There have been previous suggestions along these lines, usually too ungrounded in practice and I rejected them, but we should dig them out. The TS experience shows these are highly useful in pragmatic interop scenarios, and I expect Fable would greatly benefit from them. |
FYI: Literal types in Python: https://peps.python.org/pep-0586/ . I would think they work mostly the same way as in TS, but syntax is a bit different. You can basically choose if you write ReadOnlyMode = Literal["r", "r+"]
WriteAndTruncateMode = Literal["w", "w+", "wt", "w+t"]
WriteNoTruncateMode = Literal["r+", "r+t"]
AppendMode = Literal["a", "a+", "at", "a+t"]
AllModes = Literal[ReadOnlyMode, WriteAndTruncateMode,
WriteNoTruncateMode, AppendMode] |
just for the record, Scala also has support for them // the following constant can only store ints from 1 to 3
val three: 1 | 2 | 3 = 3
val one: 1 = 1 // val declaration
def foo(x: 1): Option[1] = Some(x) // param type, type arg
def bar[T <: 1](t: T): T = t // type parameter bound
foo(1: 1) // type ascription |
This may be required for expressing Nullable Reference Types Suggestion in F# as literal Union // Scala
val x: String = null // error: found `Null`, but required `String`
val y: String | Null = null // ok // typescript
const x: string = null // Type 'null' is not assignable to type 'string'.
const y: string | null = null //ok both Scala and Typescript have literal types, so Nullable types as unions emerge naturally, as opposed to C# or Kotlin where unions are not available yet |
I have landed here from #608 Does this suggestion cover using integer literals in types? This would allow for encoding dimensions. For example: let u : Vector<2, float> = v2 3.0 4.0
let v : Vector<3, float> = v3 1.0 2.0 3.0
let w = u + v // Compile-time error |
.NET 9 introduces const generics which is highly relevant here. C# will also include a design about this. We can refer to them for implementing this feature. |
It's not decided yet. Hasn't been discussed, championed, or even reviewed. I will be surprised if it'll make it to 9 tbh. Also, if we'll be using it, we'll be constraining runtime version, need to keep it in mind. |
@vzarytovskii The only platform that we'll leave out is effectively only .NET Framework since people upgrade to newer .NET runtimes nowadays. |
Not in my experience, unfortunately. Recently was working with one of biggest F# customers, which just migrated to net6. |
@vzarytovskii By the time this is implemented, .NET 10 would have been in LTS for years already. |
I was actually thinking taking this and anonymous DUs for F#9 |
@vzarytovskii |
I can probably join ldm when they're going to be discussing it, but probably worth asking question in the runtime suggestion |
Any development on this, fellas? |
No, it lacks approval, design and RFC, so not really actionable. |
Kinda glad, literal types feel really cool until you start dealing with two different DU with the same literal types. It's a little extra typing but that typing provides disambiguation, and it does not take long to have two literal DU with the same cases. In fact I see an example in this thread. |
What other advantages would literal types in F# bring? I say: (as good as) none. Only through the combination of set-like types and their structural character, narrowing and a bottom type do literal types unfold power. In TypeScript, all these features exist, which then allows DUs and exhaustiveness checks to be encoded. In F#, we already have DUs, and: Without having the mentioned features - apart from other trivial things - I can't think of any other useful examples. Are there any? |
Working around this is very easy: just declare DU in 1 line and if, let's say, you'd like to have a numeric literal as type, you can just create a |
This will greatly help custom DSL for views, for example Oxpecker.ViewEngine: input(type'="text") // compiles fine
input(type'="abcd") // compilation error, since it will be literal union violation |
I propose we implement something like what's known as Literal Types in TypeScript:
However, as in TypeScript, it should not be limited to literals. In fact, TS accepts any type here:
As someone who is actively working on an application for the last couple of months which uses TS for the frontend and F# for the backend, I find myself often missing this capability when switching from TS to F#. Right now, I have to explicitly define a discriminated union to do that in F#:
Instead, it would be really neat to define that DU "on the fly" or "inline" (not sure what the proper wording might be) as in TypeScript:
Pros and Cons
Advantages: More flexibility, I guess.
Disadvantages: As always, it's not a silver bullet. There are plenty of reasons one may want to define the type of that parameter explicitly, e.g., to reuse it elsewhere.
Affidavit
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: