Why cant we conform an enum to another enum?

I have a enum without a RawRepresentable.

enum A { 
    case foo(data: Data)
    case bar(task: (()-> Void))
}

Now, I want to make another enum which extends the cases in A (because cases in A always occur in B) as such

enum B : A { 
    case printGreeting(message: String)
}

But we get the errors,

1. 'B' declares raw type 'A', but does not conform to RawRepresentable and conformance could not be synthesized
2. Raw type 'A' is not expressible by any literal

Why is this not allowed when we simply use enums without RawRepresentable types just for switching over cases? How does the Enum synthesizing process work?

Even if we create the enums with the RawRepresentable as such,

enum Foo : Int {
   case a = 1
   case b = 2
}

enum Bar : Foo {
   typealias RawValue = Int

   case c = 3
}

We get the error,

1. Raw type 'Foo' is not expressible by any literal

Which doesn't make sense since Foo has Int literal. This hinders enumerable data in all forms to be ontologically mapped.

If I conform Bar to Int, Foo, you get the error

2. Multiple enum raw types 'Int' and 'Foo'

enums are not inheritable.
enum B : A just means B's raw value type is A.

If you need such enum, your B must be like below.

enum A { 
    case foo(data: Data)
    case bar(task: (()-> Void))
}

enum B {
    case aCases(A)
    case printGreeting(message: String)
}

Then B can contain A's cases.

And it seems you are confused with the word expressible.
There's ExpressibleByIntegerLiteral which Int is conformed but Foo is not.
RawRepresentable only provides init(rawValue:) and not expressible by any literals.

let foo: Foo = 3 // error: raw value for enum case must be a literal

Even if Foo conforms to ExpressibleByIntegerLiteral, Bar can't be extension of Foo.

enum Foo : Int, ExpressibleByIntegerLiteral {
    case a = 1
    case b = 2

    init(integerLiteral value: Int) {
        switch value {
        case 1:
            self = .a
        case 2:
            self = .b
        default:
            fatalError("Invalid value: \(value)")
        }
    }
}

enum Bar : Foo {
    case c = 3
}

//let bar = Bar.a  // error: type 'Bar' has no member 'a'
let bar = Bar.c
// print(bar.rawValue)  // Fatal error: Invalid value: 3
3 Likes

IIRC, it's mostly been an issue of bad timing... Whenever it's come up there were other, bigger proposals that'd captured the community's attention.

Terms of Service

Privacy Policy

Cookie Policy