You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a type has a type property or subtype named "self", you can access it using backticks (MyClassOrEnum.`self`) to disambiguate and reference the property/subtype instead of the reserved keyword.
That behaviour is not true for subtypes named "Type" though: even when adding backticks around it (MyClassOrEnum.`Type`) when trying to access the subtype, the compiler will still access the meta-type (MyClassOrEnum.Type) instead of the inner subtype.
This is both a problem as we then have no way of referencing any subtype named `Type` but also because this behaviour is inconsistent with the rest of the reserved keywords like `self`.
Example code 1: subtypes named "self" & "Type"
publicenumConstants {
publicenum `self` {
publicstaticletcontrol = "Self-Control"publicstaticletservice = "Self-Service"
}
publicenum `Type` {
publicstaticletsafety = "Type-safety"
}
}
print(Constants.self.control) // Type 'Constants' has no member 'control' (expected)print(Constants.`self`.control) // This works fineprint(Constants.Type.safety) // Type 'Constants.Type' has no member 'safety' (expected)print(Constants.`Type`.safety) // Type 'Constants.Type' has no member 'safety' (what?)
Example code 2: type properties named "self" & "Type"
publicenumConstants{staticlet`self`="me"staticlet`Type`="kind"}// Trying to print "me"print(Constants.self)// Compiles, print "Constants" (the name of the type itself)print(Constants.`self`)// Works as expected, prints "me"// Trying to print "kind":print(Constants.Type)// expected error: "Expected member name or constructor call after type name". Fix-it: "use '.self' to reference the type object"print(Constants.Type.self)// applying the fixit: prints "Constants.Type" (the name of the metatype), valid but not what we wantprint(Constants.`Type`)// unexpectedly, same error as above (while we expected to escape Type and reference its value "kind")// So… no way to print the value "kind" from Constants.`Type` then
The text was updated successfully, but these errors were encountered:
Environment
Xcode 10.1, Swift 4.2
(Still true in Swift 5.0 early beta so far)
Additional Detail from JIRA
md5: e89a888a494ee4ac9e32ecde9fdbb1b6
duplicates:
Type
should be better dealt withrelates to:
Type
propertyIssue Description:
Issue Description
If a type has a type property or subtype named "
self
", you can access it using backticks (MyClassOrEnum.`self`
) to disambiguate and reference the property/subtype instead of the reserved keyword.That behaviour is not true for subtypes named "
Type
" though: even when adding backticks around it (MyClassOrEnum.`Type`
) when trying to access the subtype, the compiler will still access the meta-type (MyClassOrEnum.Type
) instead of the inner subtype.This is both a problem as we then have no way of referencing any subtype named
`Type`
but also because this behaviour is inconsistent with the rest of the reserved keywords like`self`
.Example code 1: subtypes named "self" & "Type"
Example code 2: type properties named "self" & "Type"
The text was updated successfully, but these errors were encountered: