Enum named `Type` in nested class does not work

For some reason, having a nested class with a nested enumeration called Typedosen't plays well with a fast compiler.

class A {

    class B {

        enum Type {
            case One
            case Two
        }

        let myC: Type

        init(myC: Type) {
            self.myC = myC
        }

    }

    func getB(myC: B.Type) -> B {
        return B(myC: myC) // ERROR 1
    }

}

let a = A()
let b = a.getB(.Two) // ERROR 2

There are two errors in the above code: 'A.B.Type' is not convertible to 'A.B.Type'and 'A.B.Type.Type' does not have a member named 'Two'.

The following cases work:

  • class B out class A
  • let b = A.B(myC: .Two)
  • enum C instead enum Type

Is this a bug in Swift or is this the intended behavior? Is a Typereserved name that we should not use?

+4
source share
2 answers

B.Type is a metatype of class B, so the compiler does not like it when you define an internal enumeration called "Type".

Type / :

class A {

    required init() {}
}

class B {
    var a: A.Type
    var aInstance: A

    init() {
        a = A.self
        aInstance = a()
    }
}
+6

, . , . :

class A {
    class B {
        enum Type {
            case One
            case Two
        }
        let myC: `Type`
        init(myC: `Type`) {
            self.myC = myC
        }
    }
    func getB(myC: B.`Type`) -> B {
        return B(myC: myC)
    }
}
+4

All Articles