Multiple Types

Exactly. I find tuples useful, thus I would find anonymous enums useful, it's as simple as that. Of course, one can make a case against the usage of tuples, that would also, dually, be a case against anonymous enums, but that's a different (but valid) matter.

In fact, tuples are actually less useful that they could be due to the lack of automatic conformance to some protocols when members conform to those, but that's a limitation of tuples that should be addressed independently. But that's not an argument against tuples per se, it's just an existing limitation.

What do you mean exactly? It's not possible to have "sealed" protocols in Swift (like sealed trait in Scala or sealed class in Kotlin), so a protocol cannot be closed over a predeterminate number of cases (that is, conforming concrete types), like an enum does, and like an union type would do, so the use case is different.

About the potential utility of union types, I wrote this post a few years ago with some examples, in the context of discussing the possibility of adding the Either type to the standard library.

3 Likes