[swift-evolution] [swift-evolution-announce] [Review] SE-0155: Normalize Enum Case Representation

Brent Royal-Gordon brent at architechies.com
Mon Feb 27 21:53:52 CST 2017



-- 
Brent Royal-Gordon
Sent from my iPhone
> On Feb 27, 2017, at 10:39 AM, Matthew Johnson via swift-evolution <swift-evolution at swift.org> wrote:

> 
> Here is an example of the kind of thing I have in mind:
> 
> enum PlayerState {
>   case stopped
>   sub case playing(with track: Track)
> 
>    // type of the playing case looks something like this:
>    struct Playing { let track: Track }
> 
>    // the case value constructor looks something like this
>    static func playing(with track: Track) -> Playing {
>        return Playing(track: track)
>    }
> }

Consider a different desugaring that leads to a different conclusion:

enum PlayerState {
  case struct stopped {
    init() {}
    // I have some truly remarkable thoughts 
    // on types like this one, which this margin
    // is too narrow to contain.
  }
  case struct playing {
    let track: Track
    init(track: Track) {
      self.track = track
    }
}

In this design, because the associated values on a case are really defining an initializer on a type, it would be against Swift conventions to use a parameter label like `with` instead of the actually-meaningful `track`. So the solution to this kind of problem is simply "Don't do that". 

-- 
Brent Royal-Gordon
Sent from my iPhone


More information about the swift-evolution mailing list