[swift-evolution] [Proposal] Powerful enumerations built upon protocols, structures and classes

Marc Knaup marc at knaup.koeln
Tue Dec 15 04:49:37 CST 2015


Dear community,

after working around several limitations of enumerations I had the thought
that it should be possible to represent their behavior using just
protocols, structures and classes.

Such an approach would have several benefits:

   - The enum/case syntax would simply be syntactic sugar for constructs
   you can also create using protocols, structures and classes which
   simplifies the type system since enumerations are no longer something
   special.
   - Enumerations gain a lot of additional functionality currently
   available only to protocols, structures and classes.
   - Enumerations and their cases have a less stronger coupling, allowing
   you for example to add an existing type as an additional case.
   - It becomes possible to refer to an enumeration case directly since
   it's now a type.
   - For the same reason enumeration cases can be constructed and passed
   around independently from their enumeration.
   - The same enumeration case could be used by multiple enumerations.
   - Enumerations can have very flexible initializers instead of just the
   default one for tuples when using Associated Values.
   - Enumerations defined in Swift 2.1 will still work with minimal
   migration.

On the other hand though there are still several open issues preventing
such an approach from being implementable. Most of them stem from current
limitations of protocols and generics as well as the lack of a
singleton-like construct for structures.

Since the proposal is rather extensive I already wrote an initial version
which outlines the changes, explains the pros and cons, solves as many
issues as possible already and lists all remaining issues to be solved.

The proposal also suggests extensions in other areas of the language in
order to overcome the several limitations of the language. These changes
may or may not be desirable and must also be evaluated carefully. If the
discussion indicates that these changes are also a good idea, then they
will be spun out to their own proposal making this one dependent on them.

I would love to hear your feedback and ideas for improvements.

https://github.com/fluidsonic/swift-evolution/blob/master/proposals/NNNN-powerful-enumerations-built-upon-protocols-structures-and-classes.md

Thank you,
  Marc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/b7aebd47/attachment.html>


More information about the swift-evolution mailing list