I’d like to create an enum (or class, or struct) named Type
in one of my objects.
This object name cannot be typed normally, as it “conflict with the ‘foo.Type’ expression”.
However, like other reserved keywords, it does compile if typed with backticks.
<code>struct Foo {
// enum Type { } //ERROR: Type member must not be named 'Type', since it would conflict with the 'foo.Type' expression
enum `Type` { // Compiles with backticks...
case x
case y
}
func test() {
let x = Foo.Type // Xcode autocomplete says: "Type: `Type`" | ERROR: Expected member name or constructor call after type name
let y = Foo.Type // Xcode autocomplete says: "Type: Foo.Type" | ERROR: Expected member name or constructor call after type name
let z = Foo.Type.self // ok
}
}
</code>
<code>struct Foo {
// enum Type { } //ERROR: Type member must not be named 'Type', since it would conflict with the 'foo.Type' expression
enum `Type` { // Compiles with backticks...
case x
case y
}
func test() {
let x = Foo.Type // Xcode autocomplete says: "Type: `Type`" | ERROR: Expected member name or constructor call after type name
let y = Foo.Type // Xcode autocomplete says: "Type: Foo.Type" | ERROR: Expected member name or constructor call after type name
let z = Foo.Type.self // ok
}
}
</code>
struct Foo {
// enum Type { } //ERROR: Type member must not be named 'Type', since it would conflict with the 'foo.Type' expression
enum `Type` { // Compiles with backticks...
case x
case y
}
func test() {
let x = Foo.Type // Xcode autocomplete says: "Type: `Type`" | ERROR: Expected member name or constructor call after type name
let y = Foo.Type // Xcode autocomplete says: "Type: Foo.Type" | ERROR: Expected member name or constructor call after type name
let z = Foo.Type.self // ok
}
}
Trying to use my Type, though, results in the errors above and still seems to conflict with the built-in Type expression. Namespacing the Type does not seem to work.
Can the name Type
be used to name a user’s Swift object?
If not, why does enum `Type`
compile?