Enum's rawValue initializer hidden, sometimes?

Anyone else getting this? It works in a playground but in a normal source file, I'm getting the error. Some others say they can't reproduce it.


The error is on line 14: Incorrect argument label in call (have 'rawValue:', expected 'name:')


enum Foo : Int {
    case A, B

    init?(name:String) {
        switch name {
        case "A": self = .A
        case "B": self = .B
        default: return nil
        }
    }
}

let foo = Foo.A
let foo2 = Foo(rawValue:2)!  // error

Just to be clear, I'm asking about a compile time error. I removed some cases to shorten the code but forgot to change the rawValue:2 to rawValue:1.

It looks like a bug, where the init(rawValue:) initializer isn't being created (or isn't available) if you have defined any other initializers. Presumably it still works in playgrounds because the line-by-line compilation creates a state where the enum exists and your custom initializers don't yet.


Unlike the other automatic initializers for other types, the documentation doesn't say that it isn't created if you define other initializers. (The documentation explicitly states that default initializers for structs and classes, and member-wise initializers for structs, aren't created if you define your own initializers.)


I would recommend filing a bug with Apple, so that either it gets fixed or the documentation gets updated to reflect its behavior (if it's intentional so that all automatically generated initializers have similar behavior).

Enum's rawValue initializer hidden, sometimes?
 
 
Q