Remix.run Logo
nielsbot 8 hours ago

I don't think it's horrible, but I really do wish they would copy TypeScript here.

Let me do this:

    const x: String | Int
Instead of

    enum MyEnum {
        case string(String)
        case int(Int)
    }
There's an existing proposal for this here:

https://forums.swift.org/t/re-proposal-type-only-unions/7270...

Mond_ 7 hours ago | parent | next [-]

It's worth pointing out that the two examples that you're writing are actually strictly different, and not just "better syntax for the same thing". (This is assuming `String | Int` works as in Python, and the second example works as in Rust.)

To understand the difference, `String | String` is just `String`. It's a union, not a sum type. There's no tag or identifier, so you cannot distinguish whether it's the first or the second string.

If this sounds pedantic, this has pretty important ramifications, especially once generics get involved.

ekimekim 6 hours ago | parent [-]

To provide a concrete example, this bit me in a typescript codebase:

    type Option<T> = T | undefined

    function f<T>(value: T): Option<T> { ... }

    let thing: string | undefined = undefined;
    let result = f(thing);
Now imagine the definition of Option is in some library or other file and you don't realize how it works. You are thinking of the Option as its own structure and expect f to return Option<string | undefined>. But Option<string | undefined> = string | undefined | undefined = string | undefined = Option<string>.

The mistake here is in how Option is defined, but it's a footgun you need to be aware of.

kibwen 6 hours ago | parent | prev [-]

Note that union types are not the same thing as sum types, even if they're somewhat similar. https://viralinstruction.com/posts/uniontypes/