2

I've got multiple enums with raw values, but I don't like having to say rawValue: every time I initialize one from a raw value, so I've supplied an alternative delegating initializer with no external label:

enum E1 : Int {
    case One, Two
    init?(_ what:Int) {
        self.init(rawValue:what)
    }
}
enum E2 : Int {
    case One, Two
    init?(_ what:Int) {
        self.init(rawValue:what)
    }
}

Very nice. I can say let e = E1(0) and the right thing happens.

Now I'd like to consolidate the repeated code. I was hoping that Swift 2.0 protocol extensions would allow me to do this - writing the init?(_ what:Int) initializer in one place and injecting / inheriting it in both enums. However, I haven't found a way that works. The problem is that the protocol extension doesn't know that the adopter will have an init(rawValue:) initializer, and I have not found a way to reassure it.

I suspect that this is because of the automagic way that the rawValue initializer comes into existence, and so probably nothing can be done. But perhaps someone has a suggestion.

3 Answers 3

6

Sounds like you're looking to extend the RawRepresentable protocol:

extension RawRepresentable {
    init?(_ what: RawValue) {
        self.init(rawValue: what)
    }
}

Any enum with a raw type automatically conforms to RawRepresentable, therefore you haven't got to make E1 or E2 conform to any extra protocols:

enum E1: Int {
    case One = 1, Two
}

enum E2: String {
    case One = "1", Two = "2"
}

let e1 = E1(1)   // .One
let e2 = E2("2") // .Two
1
  • 2
    Simple but (for me) revolutionary - it just didn't occur to me. Actually, it's even better than that. I can say extension RawRepresentable where Self:Attribute, and now I'm doing what I really want to do, which is (1) do this only for certain enums, and (2) keep the ability to subsume my parallel enums under a common type.
    – matt
    Jul 4, 2015 at 0:06
0

I was able to get it to work, like this:

protocol P {
    var rawValue : Int {get}
    init?(rawValue: Int)
}
extension P {
    init?(_ what:Int) {
        self.init(rawValue:what)
    }
}

enum E1 : Int, P {
    case One, Two
}
enum E2 : Int, P {
    case One, Two
}

We use the regular protocol declaration to reassure the protocol extension that our adopter will have an init?(rawValue:).

[However, there is still something very odd going on. This code compiles in one of my projects, but not in another. I think it has something to do with how other code uses the P protocol; if no other code uses the P protocol, all is well, but if other code uses the P protocol, in ways that I have not precisely determined, the code fails to compile, with a mysterious error about the enum not conforming to the protocol.]

1
  • Perhaps you have another protocol named P hiding somewhere in your code.
    – hashemi
    Jul 3, 2015 at 12:45
0

Dunno what can be wrong. Would be nice to have isolated case. Maybe more generic version helps?

protocol BaseRaw {
  typealias T
  var rawValue : T { get }
  init?(rawValue: T)
}

extension BaseRaw {
  init?(_ value: T) {
    self.init(rawValue: value)
  }
}

enum E1 : Int, BaseRaw {
  case One = 1
  case Two = 2
}

enum E2 : String, BaseRaw {
  case One = "1"
  case Two = "2"
}

let e = E1(1)
let e2 = E2("2")

print(e)            // Optional(E1.One)
print(e!.rawValue)  // 1
print(e2)           // Optional(E2.Two)
print(e2!.rawValue) // "2"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.