I don't know if there's a great comprehensive reference for this syntax but the basic idea is that if there's a situation where the compiler knows what the type of an expression 'should' be (i.e., it can deduce the type via inference) then you can access static members on that type without writing the type name explicitly. I think the first (and possibly only?) mention of this syntax in TSPL is in the Enumerations section:
The type of
directionToHead
is inferred when it’s initialized with one of the possible values ofCompassPoint
. OncedirectionToHead
is declared as aCompassPoint
, you can set it to a differentCompassPoint
value using a shorter dot syntax:directionToHead = .east
The type of
directionToHead
is already known, and so you can drop the type when setting its value. This makes for highly readable code when working with explicitly typed enumeration values.
Note that the example you've written above:
Will not in general compile because the compiler has no information about what the type of the expression 'should' be. OTOH, if you write:
let x: MyEnum = .someOtherCase
or
var x = MyEnum.someCase
x = .someOtherCase
then the compiler knows that the expression on the right hand side of the =
'should' have type MyEnum
, so it can figure out that you're referring to MyEnum.someOtherCase
.
Also note that there are recent and ongoing developments in this area to increase the number of cases where the compiler is able to infer the base type, and ongoing bug fixes to improve type inference may open up additional possibilities as well!