Home > Enterprise >  How to disambiguate
How to disambiguate

Time:06-08

I have what looks like a simple namespace/scope question here. I remember the Swift designers discussing the use of some kind of annotation like @.... that could solve this type issue. How do I disambiguate

class A {
}

class B {
  enum BEnum {}
}

extension A {
   enum B {
       static let x = B.BEnum // Type 'A.B' has no member 'BEnum'
   }
}

CodePudding user response:

As @Alexander has highlighted, you can use the module name that defines B. Since B is likely to be defined in your project, you can use the project's name to access the top-level class:

class A {
}

class B {
  enum BEnum {}
}

extension A {
   enum B {
     static let x = ProjectName.B.BEnum.self
   }
}

Alternatively, if you are defining a package, replace ProjectName with your ModuleName.

  • Related