Nested type in class can't named "Type"

public class Axis {
public enum Type { //error: type member must not be named ‘Type’, since it would conflict with the ‘foo.Type’ expression
case x, y
}
}

It’s very common to call a nested type named “Type”, but this is not works in Swift. Any thoughts?

1 Like

Hello, what about Kind instead?

em, that’s a good suggestion. Since it’s so common to name a nested type to “Type”, what if we change current “Type” to “Class” or some other name…

Yes. Since type, class, a few others, are reserved and won’t be used easily, I often settle on the quite acceptable kind, and move on to my next task.

Sometimes, though, you really want to use a reserved identifier. In this case, you can use backticks. For example:

enum AlertActionStyle {
    case `default`, cancel, destructive
}
1 Like

Got it, thank you @gwendal.roue

Type indeed conflicts with foo.Type. If you were to refer to the metatype of Axis (Axis.Type) , it would conflict with the nested type you named Type, which is also referred to as Axis.Type.

P.S. This should be somehow moved to “Using Swift”.

I used TType instead.

Maybe someone knowledgeable on the inner workings of the type system can comment on the feasibility of using Type, or Type as the metatype in the language and not Axis.Type / MyType.Type. Similar to how Mirror works.

As a side point, it would allow the global type(of:) function to be moved to Swift.Type.of(), which is an improvement in my opinion.

Perhaps Metatype?

Feel free to jump right into this discussion.