[swift-evolution] Consolidate Code for Each Case in Enum

Derrick Ho wh1pch81n at gmail.com
Sun Jan 8 13:22:28 CST 2017


Currently we can write a helper method to aid in getting the values inside
the enum associated value.  Below is a fully working implementation:

```

enum Package {

case box(String, Int)

case circular(String)

var associated: Associated {

return Associated(package: self)

}

struct Associated {

let box: (String, Int)?

let circular: (String)?

init(package: Package) {

switch package {

case .box(let b):

box = b

circular = nil

case .circular(let b):

box = nil

circular = b

}

}

}

}


let b = Package.box("square", 5)

b.associated.box?.0 // Optional("square")

b.associated.box?.1 // Optional(5)

b.associated.circular // nil


let c = Package.circular("round")

c.associated.box?.0 // nil

c.associated.box?.1 // nil

c.associated.circular // Optional("round")
```

I had to wedge in a special type called "Associated" and had to write some
boiler-plate code to get this effect.  It is quite predictable and can
probably be done under the hood.  I would of course prefer syntactic sugar
to simplify it and turn
```
b.associated.box?.0
```
into
```
b.box?.0
```

On Sun, Jan 8, 2017 at 1:05 PM David Sweeris via swift-evolution <
swift-evolution at swift.org> wrote:

>
> On Jan 8, 2017, at 06:53, Karim Nassar via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> One area of enums that I’d love to see some sugar wrapped around (and
> perhaps this has already been discussed previously?) is extracting
> associated values.
>
> There are many times where, given an enum like:
>
> enum Feedback {
> case ok
> case info(String)
> case warning(String, Location)
> case error(String, Location)
> }
>
> I’d love it if we could tag the associated values with some semantic
> accessor, perhaps borrowed from tuples:
>
> enum Feedback {
> case ok
> case info(msg: String)
> case warning(msg: String, loc: Location)
> case error(msg: String, loc: Location)
> }
>
> then:
>
> let foo = self.getSomeFeedback() // -> Feedback
> if let msg = foo.msg { // since not all cases can hold a ‘msg’ .msg is an
> Optional
> print(foo)
> }
>
>
> Can't remember if it's come up before, but +1. I can't count how many
> times I've written something like:
> enum Foo : CustomStringConvertible {
>     case c1(T1)
>     case c2(T2)
>     ...
>     case cN(TN)
>
>     var description: String {
>         switch self {
>             case .c1(let val): return "\(val)"
>             case .c2(let val): return "\(val)"
>             ...
>             case .cN(let val): return "\(val)"
>         }
>     }
> }
>
> Being able to simplify that to:
> var description: String {
>     let nilDesc = "some appropriate description"
>     return "\(self.0 ?? nilDesc)"
> }
>
> Would be great.
>
> - Dave Sweeris
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170108/fc3a4e19/attachment.html>


More information about the swift-evolution mailing list