Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Swift syntax " .bar" called?

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 enums:

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?

like image 418
Ky. Avatar asked Oct 30 '17 20:10

Ky.


1 Answers

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
like image 193
Hamish Avatar answered Nov 04 '22 23:11

Hamish