Swift has this handy syntax:
enum Foo {
case bar
case baz
}
func hoge(foo: Foo) {
}
hoge(foo: .bar) // This
Which is mirrored in places other than enum
s:
struct Qux {
static let `default` = Qux()
}
func hoge(qux: Qux) {
}
hoge(qux: .default) // This
I am not sure what to call this in conversation / tickets. Maybe "type-inferred dot syntax"? I'm unsure. Does this syntax have an official name? If so, what is it?
It is called an implicit member expression. From the grammar section of the language guide:
An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a type method, in a context where type inference can determine the implied type. It has the following form:
.
member name
For example:
var x = MyEnumeration.someValue x = .anotherValue
From Apple's Swift book:
The values defined in an enumeration (such as north, south, east, and west) are its enumeration cases.
来源:https://stackoverflow.com/questions/47023274/what-is-the-swift-syntax-bar-called