3 ms·
Associated values in enums make sense, if you look at the enum as a class/struct, under the hood. It's quite powerful, once you get your head around it. I wri
by ChrisMarshallNY 7y ago
Associated values in enums make sense, if you look at the enum as a class/struct, under the hood.
It's quite powerful, once you get your head around it.
I write about that here: https://littlegreenviper.com/miscellany/swiftwater/enums-with-associated-values/ https://littlegreenviper.com/miscellany/swiftwater/enums-wit...
And here: https://littlegreenviper.com/miscellany/swiftwater/writing-a-robust-little-parser-in-swift-epilogue/#peek https://littlegreenviper.com/miscellany/swiftwater/writing-a...
- lmm 7y ago> Associated values in enums make sense, if you look at the enum as a class/struct, under the hood. But they don't make sense as enums. If you have to look "under the hood" to get a reasonable model of a feature, it's a bad design. > It's quite powerful, once you get your head around it. > I write about that here: https://littlegreenviper.com/miscellany/swiftwater/enums-wit.. https://littlegreenviper.com/miscellany/swiftwater/enums-wit.... Like I said, you can write the same kind of thing very easily in Kotlin by using a sealed class - your example would translate line-for-line. It's a useful feature. But it's very confusing to call it a kind of enum rather than as a kind of class; as your other post acknowledges, it means Swift essentially has two different things that are differently implemented but both called "enum".
- ChrisMarshallNY 7y agoYes. The enum behaves quite differently, depending on whether or not it has associated values.