What is the Swift syntax “ .bar” called?

后端 未结 2 2059
孤独总比滥情好
孤独总比滥情好 2020-12-11 20:01

Swift has this handy syntax:

enum Foo {
    case bar
    case baz
}


func hoge(foo: Foo) {
}


hoge(foo: .bar) //          


        
2条回答
  •  星月不相逢
    2020-12-11 20:16

    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
    

提交回复
热议问题