top | item 46841793

(no title)

JackYoustra | 29 days ago

Adding on, its also a bit much to say that Swift has a good level of sugar and reference an enum of all things. Swift's union type story is so poor that, instead of properly modeling state, people to this day still use masses of optional types in their views rather than an enum because actually using swift enums and protocols is so painful.

discuss

order

frizlab|29 days ago

I don’t which Swift developers you are talking to, but let the record know I don’t and use enum quite a lot. And I don’t find it particularly painful though a bit verbose at times.

willtemperley|29 days ago

I think Swift enums are really well designed, but SwiftData doesn't really support them unfortunately and Observable has problems with reactivity and enums (works on iOS not macOS I've found).

So lots of optionals might well be the better path here.

nielsbot|29 days 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_|29 days ago

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.

vor_|28 days ago

> actually using swift enums and protocols is so painful.

In what way? My understanding is they're widely used and encouraged.

JackYoustra|28 days ago

If you look at most swiftui views, there will be a mass of optionals, rather than enum cases for every possible state that the view can occupy and a state machine transitioning between each state.

If you actually try and write it all out (like go full-on and use TCA to model all the states and actions such that all and only valid states can be represented in the state types) the compiler is going to have a hard time, you're going to write masses of boilerplate, and just in general it's much rougher than zustand and expo.