Swift语法是什么? .bar"叫? [英] What is the Swift syntax " .bar" called?

查看:96
本文介绍了Swift语法是什么? .bar"叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift具有以下方便的语法:

Swift has this handy syntax:

enum Foo {
    case bar
    case baz
}


func hoge(foo: Foo) {
}


hoge(foo: .bar) // This

enum以外的其他地方被镜像:

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?

推荐答案

它称为隐式成员表达式.来自

隐式成员表达式是访问成员的缩写方式 在上下文中的类型,例如枚举用例或类型方法 类型推断可以确定隐式类型.它具有 格式如下:

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

例如:

var x = MyEnumeration.someValue
x = .anotherValue

这篇关于Swift语法是什么? .bar"叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆