Switch on an optional enum -- `case: nil`?


(Eric Miller) #1

Wondering if you guys think this is a reasonable idea.

I was switching on an optional enum today, and naturally gravitated towards
including nil as a case:

enum Coin {
  case heads
  case tails
}
var result: Coin?

switch result {
case heads: print("heads")
case tails: print("tails")
case nil: print("not yet flipped") // exhaustive
}

Doesn't compile, and of course I can if-let or guard to unwrap instead, but
I like the compactness of this.


(David Sweeris) #2

So… Is your idea that wrapping a SomeEnum in an Optional would add some “invalid” case to SomeEnum, or that switching on a SomeEnum? would be implicitly rewrite:
switch result {
case .heads: print("heads")
case .tails: print("tails")
case .none: print("not yet flipped") // exhaustive
}
to:
switch result {
case .none: print("not yet flipped")
case .some(let _result):
  switch _result {
  case .heads: print("heads")
  case .tails: print("tails")
  }
}

···

On May 5, 2016, at 10:40 AM, Eric Miller via swift-evolution <swift-evolution@swift.org> wrote:

Wondering if you guys think this is a reasonable idea.

I was switching on an optional enum today, and naturally gravitated towards including nil as a case:

enum Coin {
  case heads
  case tails
}
var result: Coin?

switch result {
case heads: print("heads")
case tails: print("tails")
case nil: print("not yet flipped") // exhaustive
}

Doesn't compile, and of course I can if-let or guard to unwrap instead, but I like the compactness of this.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Krzysztof Siejkowski) #3

It doesn’t compile because you’re switching on the Optional and trying to pattern match it to Coin.

It compiles right when you pattern match it to Optional<Coin>:

switch result {
case .heads?: print("heads")
case .tails?: print("tails")
case nil: print("not yet flipped")
}

Best,
Krzysztof

···

On 5 May 2016 at 17:42:24, Eric Miller via swift-evolution (swift-evolution@swift.org) wrote:

Wondering if you guys think this is a reasonable idea.

I was switching on an optional enum today, and naturally gravitated towards including nil as a case:

enum Coin {
case heads
case tails
}
var result: Coin?

switch result {
case heads: print("heads")
case tails: print("tails")
case nil: print("not yet flipped") // exhaustive
}

Doesn't compile, and of course I can if-let or guard to unwrap instead, but I like the compactness of this.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Eric Miller) #4

Nor I. This syntax is excellent, thanks Krzysztof.

···

On Thu, May 5, 2016 at 9:56 AM, David Sweeris <davesweeris@mac.com> wrote:

Oh, cool! I didn’t realize this syntax existed.

On May 5, 2016, at 10:52 AM, Krzysztof Siejkowski via swift-evolution < > swift-evolution@swift.org> wrote:

It doesn’t compile because you’re switching on the Optional and trying to
pattern match it to Coin.

It compiles right when you pattern match it to Optional<Coin>:

switch result {
case .heads?: print("heads")
case .tails?: print("tails")
case nil: print("not yet flipped")
}

Best,
Krzysztof

On 5 May 2016 at 17:42:24, Eric Miller via swift-evolution ( > swift-evolution@swift.org) wrote:

Wondering if you guys think this is a reasonable idea.

I was switching on an optional enum today, and naturally gravitated
towards including nil as a case:

enum Coin {
  case heads
  case tails
}
var result: Coin?

switch result {
case heads: print("heads")
case tails: print("tails")
case nil: print("not yet flipped") // exhaustive
}

Doesn't compile, and of course I can if-let or guard to unwrap instead,
but I like the compactness of this.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(David Sweeris) #5

Oh, cool! I didn’t realize this syntax existed.

···

On May 5, 2016, at 10:52 AM, Krzysztof Siejkowski via swift-evolution <swift-evolution@swift.org> wrote:

It doesn’t compile because you’re switching on the Optional and trying to pattern match it to Coin.

It compiles right when you pattern match it to Optional<Coin>:

switch result {
case .heads?: print("heads")
case .tails?: print("tails")
case nil: print("not yet flipped")
}

Best,
Krzysztof

On 5 May 2016 at 17:42:24, Eric Miller via swift-evolution (swift-evolution@swift.org <mailto:swift-evolution@swift.org>) wrote:

Wondering if you guys think this is a reasonable idea.

I was switching on an optional enum today, and naturally gravitated towards including nil as a case:

enum Coin {
  case heads
  case tails
}
var result: Coin?

switch result {
case heads: print("heads")
case tails: print("tails")
case nil: print("not yet flipped") // exhaustive
}

Doesn't compile, and of course I can if-let or guard to unwrap instead, but I like the compactness of this.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Ross O'Brien) #6

I was about to provide this, but Krzysztof's example is more compact than
mine:

enum Coin {

case heads

case tails

}

var result: Coin?

switch result {

case .Some(.heads): print("heads")

case .Some(.tails): print("tails")

case nil: print("not yet flipped") // exhaustive

}

···

On Thu, May 5, 2016 at 4:52 PM, Krzysztof Siejkowski via swift-evolution < swift-evolution@swift.org> wrote:

It doesn’t compile because you’re switching on the Optional and trying to
pattern match it to Coin.

It compiles right when you pattern match it to Optional<Coin>:

switch result {

case .heads?: print("heads")

case .tails?: print("tails")

case nil: print("not yet flipped")

}

Best,
Krzysztof

On 5 May 2016 at 17:42:24, Eric Miller via swift-evolution ( > swift-evolution@swift.org) wrote:

Wondering if you guys think this is a reasonable idea.

I was switching on an optional enum today, and naturally gravitated
towards including nil as a case:

enum Coin {
  case heads
  case tails
}
var result: Coin?

switch result {
case heads: print("heads")
case tails: print("tails")
case nil: print("not yet flipped") // exhaustive
}

Doesn't compile, and of course I can if-let or guard to unwrap instead,
but I like the compactness of this.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution