How in Swift specify type constraint to be enum?

enums, swift

Solution

enum SomeEnum: Int {
    case One, Two, Three
}

class SomeClass<E: RawRepresentable where E.RawValue == Int>{
    func doSomething(e: E) {
        print(e.rawValue)
    }
}

class SomeEnumClass : SomeClass<SomeEnum> {

}

or directly

class SomeOtherClass{
    func doSomething<E: RawRepresentable where E.RawValue == Int>(e: E) {
        print(e.rawValue)
    }
}

UPDATE for swift3:

enum SomeEnum: Int {
    case One, Two, Three
}

class SomeClass<E: RawRepresentable> where E.RawValue == Int {
    func doSomething(e: E) {
        print(e.rawValue)
    }
}

class SomeEnumClass : SomeClass<SomeEnum> {

}

resp.

class SomeOtherClass{
    func doSomething<E: RawRepresentable>(e: E) where E.RawValue == Int {
        print(e.rawValue)
    }
}

Problem

I want to specify a type constraint that the type should be a raw value enum: ``` enum SomeEnum: Int { case One, Two, Three } class SomeProtocol<E: enum<Int>> { // <- won't compile func doSomething(e: E) { compute(e.toRaw()) } } ``` How can I do it in Swift? (I used the F# syntax for example)

Original source