[swift-evolution] [Proposal] Enums with static stored properties for each case

Paul Cantrell cantrell at pobox.com
Wed Jun 1 10:10:48 CDT 2016


> On Jun 1, 2016, at 9:48 AM, David Waite via swift-evolution <swift-evolution at swift.org> wrote:
> 
> One thing I did often in Java (and miss in Swift) is using their enums to build state machines or implement command patterns for common commands.
> 
> Java enums are a sealed set of subclasses of the enum base type with (hopefully) immutable, singleton instances. So you can do fun things like:
> - Define the base class constructor to be called to instantiate the subclasses, and declare the cases with the constructor arguments
> - Declare a method on the base type and refine it on 1-2 particular cases
> - Declare the enum implements an interface, and implement that interface separately for each case.
> - Define data accessors specific to the type (such as the planets example above)

+1 to this behavior — one of the increasingly rare places where Java is nicer than Swift. This is the natural generalization / next step of the stored properties of this thread, and I can confirm what David said: it’s useful in practice.

P



More information about the swift-evolution mailing list