Skip to content
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

Open
5 tasks done
aspnetde opened this issue Oct 21, 2022 · 23 comments
Open
5 tasks done

Literals as types #1195

aspnetde opened this issue Oct 21, 2022 · 23 comments

Comments

@aspnetde
Copy link

aspnetde commented Oct 21, 2022

I propose we implement something like what's known as Literal Types in TypeScript:

const foo = (bar: "A" | "B") => {}

foo("A"); // ok
foo("C"); // forbidden

However, as in TypeScript, it should not be limited to literals. In fact, TS accepts any type here:

interface Options {
  width: number;
}
function configure(x: Options | "auto") {}
configure({ width: 100 }); // ok
configure("auto"); // ok
configure("automatic"); // forbidden

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#:

type Bar = A | B
let foo (bar: Bar) = ()
foo A

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:

let foo (bar: A | B) = ()

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:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

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.

@kerams
Copy link

kerams commented Oct 21, 2022

#538
dotnet/fsharp#12265

@aspnetde
Copy link
Author

#538 dotnet/fsharp#12265

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.

@dsyme
Copy link
Collaborator

dsyme commented Oct 21, 2022

The suggestion here is misnamed, it's actually for typescript style literals-as-types

@dsyme
Copy link
Collaborator

dsyme commented Oct 21, 2022

@dsyme dsyme changed the title Allow to define Discriminated Unions within Function Signatures Literals as types Oct 21, 2022
@dsyme dsyme reopened this Oct 21, 2022
@dsyme
Copy link
Collaborator

dsyme commented Oct 21, 2022

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.

@dbrattli
Copy link

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 Literal["a", "b"] or Literal["a"] | Literal["b"]. They have the exact same meaning. They can also be composed, e.g:

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]

@dsyme dsyme mentioned this issue Oct 26, 2022
5 tasks
@3xau1o
Copy link

3xau1o commented Oct 28, 2022

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

@3xau1o
Copy link

3xau1o commented Apr 26, 2023

This may be required for expressing Nullable Reference Types Suggestion in F# as literal Union string | null, instead of string? the same way Scala and Typescript do

// 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

@njlr
Copy link

njlr commented May 27, 2023

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

@Happypig375
Copy link
Contributor

.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.

@vzarytovskii
Copy link

.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.

@Happypig375
Copy link
Contributor

@vzarytovskii The only platform that we'll leave out is effectively only .NET Framework since people upgrade to newer .NET runtimes nowadays.

@vzarytovskii
Copy link

vzarytovskii commented Aug 7, 2023

@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.

@Happypig375
Copy link
Contributor

@vzarytovskii By the time this is implemented, .NET 10 would have been in LTS for years already.

@vzarytovskii
Copy link

@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

@Happypig375
Copy link
Contributor

Happypig375 commented Aug 7, 2023

@vzarytovskii
If you have influence over runtime and C# design then probably you can ensure that polyfills are allowed across C# and F#. F# can then implement with polyfills, similar to nullability attributes in an unsupported runtime.

@vzarytovskii
Copy link

@vzarytovskii
If you have influence over runtime and C# design then probably you can ensure that polyfills are allowed across C# and F#. F# can then implement with polyfills.

I can probably join ldm when they're going to be discussing it, but probably worth asking question in the runtime suggestion

@LeaveNhA
Copy link

Any development on this, fellas?

@vzarytovskii
Copy link

Any development on this, fellas?

No, it lacks approval, design and RFC, so not really actionable.

@voronoipotato
Copy link

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.

@SchlenkR
Copy link

The TS experience shows these are highly useful in pragmatic interop scenarios [...]

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?

@atsapura
Copy link

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.ToInt() method. What's the point of polluting the language?

@Lanayx
Copy link

Lanayx commented Nov 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests