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

Matthew Johnson matthew at anandabits.com
Thu May 26 17:01:41 CDT 2016


> On May 26, 2016, at 4:47 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> The proposed solution is to have single static initializer for each
>> enum case that initializes stored properties. For example,
> 
> My opinions so far:
> 
> - Abusing rawValue is just that: an abuse.
> 
> - Using `where` just doesn't match the use of `where` elsewhere in the language; everywhere else, it's some kind of condition.
> 
> - Dictionaries are the most straightforward way to handle this with the current language, but their lack of exhaustiveness checking is a problem.
> 
> What I would do is borrow the "accessors" concept from the property behaviors proposal and extend it so that it supported both functions and variables. Then I would let you write this:
> 
> 	enum Planet {
> 	   accessor var mass: Float
> 	   accessor var radius: Float
> 	   
> 	   case mercury {
> 	      mass = 3.303e+23
> 	      radius = 2.4397e6
> 	   }
> 	   case venus {
> 	      mass = 4.869e+24
> 	      radius = 6.0518e6
> 	   }
> 	   case earth {
> 	      mass = 5.976e+24
> 	      radius = 6.37814e6
> 	   }
> 	   case mars {
> 	      mass = 6.421e+23
> 	      radius = 3.3972e6
> 	   }
> 	   case jupiter {
> 	      mass = 1.9e+27
> 	      radius = 7.1492e7
> 	   }
> 	   case saturn {
> 	      mass = 5.688e+26
> 	      radius = 6.0268e7
> 	   }
> 	   case uranus {
> 	      mass = 8.686e+25
> 	      radius = 2.5559e7
> 	   }
> 	   case neptune {
> 	      mass = 1.024e+26
> 	      radius = 2.4746e7
> 	   }
> 	}
> 
> You would also be able to declare methods like this; each implementation would just look like `methodName { code }`. And you could provide default implementations too:
> 
> 	enum Planet {
> 	   accessor var mass: Float
> 	   accessor var radius: Float
> 	   accessor var habitable: Bool = false
> 	   
> 	   case mercury {
> 	      mass = 3.303e+23
> 	      radius = 2.4397e6
> 	   }
> 	   case venus {
> 	      mass = 4.869e+24
> 	      radius = 6.0518e6
> 	   }
> 	   case earth {
> 	      mass = 5.976e+24
> 	      radius = 6.37814e6
> 	      habitable = true
> 	   }
> 	   case mars {
> 	      mass = 6.421e+23
> 	      radius = 3.3972e6
> 	   }
> 	   case jupiter {
> 	      mass = 1.9e+27
> 	      radius = 7.1492e7
> 	   }
> 	   case saturn {
> 	      mass = 5.688e+26
> 	      radius = 6.0268e7
> 	   }
> 	   case uranus {
> 	      mass = 8.686e+25
> 	      radius = 2.5559e7
> 	   }
> 	   case neptune {
> 	      mass = 1.024e+26
> 	      radius = 2.4746e7
> 	   }
> 	}
> 

This is the first really interesting (to me) idea in the thread.  I think I like it but need to give it more thought to decide for sure. 

One really interesting thing we could do to build on this would be to introduce the associated value names inside the scope of each case as if they were member variables:

enum Foo {
    accessor func bar() -> Int
    
    case baz(val: Int) {
        // val is in scope here
        func bar() {
            return val
        }
    }
}



> -- 
> Brent Royal-Gordon
> Architechies
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list