[Pitch] Numeric enum cases


(Adrian Zubarev) #1

Have we already discussed this somewhere?

enum MyEnum {
    case 0
    case 1(String)
}

// or

enum MyEnum {
    case `0`
    case `1`(String)
}

let zero: MyEnum = .0
let one = MyEnum.1("Swift")

switch zero {
     
case .0:
   print("zero")

case .1(let string)
   print(string)
}

Tuples have this .# syntax, where the number is the index of the tuple value. Can’t we allow this on enum cases as well?

This is additive and something for phase 2, but I’d like to know if we want to allow this?! :slight_smile:

···

--
Adrian Zubarev
Sent with Airmail


(Saagar Jha) #2

I'm curious about the use case for this. For tuples it's a positional
argument; how does this apply to enum cases which shouldn't depend on their
order?

···

On Tue, Nov 22, 2016 at 05:43 Adrian Zubarev via swift-evolution < swift-evolution@swift.org> wrote:

Have we already discussed this somewhere?

enum MyEnum {
    case 0
    case 1(String)
}

// or

enum MyEnum {
    case `0`
    case `1`(String)
}

let zero: MyEnum = .0
let one = MyEnum.1("Swift")

switch zero {

case .0:
   print("zero")

case .1(let string)
   print(string)
}

Tuples have this .# syntax, where the number is the index of the tuple
value. Can’t we allow this on enum cases as well?
------------------------------

This is additive and something for phase 2, but I’d like to know if we
want to allow this?! :slight_smile:

--
Adrian Zubarev
Sent with Airmail

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


(Adrian Zubarev) #3

The general idea of allowing this came from another thread that I started:

[swift-evolution] [Discussion] Parameter ‘vector’ keyword vs. triple dot prefix for variadic generics

Another real world use case I could think of is to put numeric error codes inside an enum.

enum TCPSocketErrorCode : String {
   case `0` = "Success"
   case `1` = "Operation not permitted"
   case `2` = "No such file or directory"
   case `3` = "No such process"
   case `4` = "Interrupted system call"
   case `5` = "Input/output error"
   case `6` = "No such device or address"
   …
}
Remember that you don’t have to provide the cases in order or incrementally. This should be a valid enum:

enum Numbers {
     case `42`
     case `13`
}

···

--
Adrian Zubarev
Sent with Airmail

Am 23. November 2016 um 01:35:01, Saagar Jha (saagar@saagarjha.com) schrieb:

I'm curious about the use case for this. For tuples it's a positional argument; how does this apply to enum cases which shouldn't depend on their order?
On Tue, Nov 22, 2016 at 05:43 Adrian Zubarev via swift-evolution <swift-evolution@swift.org> wrote:
Have we already discussed this somewhere?

enum MyEnum {
    case 0
    case 1(String)
}

// or

enum MyEnum {
    case `0`
    case `1`(String)
}

let zero: MyEnum = .0
let one = MyEnum.1("Swift")

switch zero {
      
case .0:
   print("zero")

case .1(let string)
   print(string)
}

Tuples have this .# syntax, where the number is the index of the tuple value. Can’t we allow this on enum cases as well?

This is additive and something for phase 2, but I’d like to know if we want to allow this?! :slight_smile:

--
Adrian Zubarev
Sent with Airmail
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution