5 ms·
Newbie here. What are the differences with Swift Enumerations?
by marvel_boy 10y ago
Newbie here. What are the differences with Swift Enumerations?
- Manishearth 10y agoSwift enums are basically the same as Rust ones. The differences between C++ variants and Rust enums listed in the blog post apply between C++ and Swift too.
- nathankleyn 10y agoCompared to Rust enumerations, they are one and the same. In fact, looking at the documentation for Swift Enumerations yields this little nugget: You can define Swift enumerations to store associated values of any given type, and the value types can be different for each case of the enumeration if needed. Enumerations similar to these are known as discriminated unions, tagged unions, or variants in other programming languages. [1] Haskell and other ML family languages also have similar constructs, although these are usually modelled using "Algebraic Data Types", or "sum types". [2] [3] [1]: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html https://developer.apple.com/library/content/documentation/Sw... [2]: https://wiki.haskell.org/Algebraic_data_type https://wiki.haskell.org/Algebraic_data_type [3]: https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/sum-types https://www.schoolofhaskell.com/school/to-infinity-and-beyon...
- masklinn 10y agoDetails (some features they provide or can provide), Swift's enumerations and Rust's enum are both implementation of the older concept of Algebraic Data Types (ADT), which can be found in pretty much any language inspired by ML (directly or indirectly).