What is the Swift syntax “ .bar” called?

后端 未结 2 2038
孤独总比滥情好
孤独总比滥情好 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
    
    0 讨论(0)
  • 2020-12-11 20:29

    From Apple's Swift book:

    The values defined in an enumeration (such as north, south, east, and west) are its enumeration cases.

    0 讨论(0)
提交回复
热议问题