Swift语法显式成员表达式

Swift grammar explicit member expression

在 Swift 书中的 grammar section 中有两个语法声明:

explicit-member-expression  ->  postfix-expression  .  decimal-digits
explicit-member-expression  ->  postfix-expression  .  identifier  generic-argument-clause[opt]

第一个用于访问元组:

var tuple = (1, 2, 3)
tuple.1 = tuple.2

第二个用于访问属性和函数等其他成员:

struct S {
    var property = 0
    func function<T>(parameter: T) {}
}

S().property
S().function(3)

但是我找不到可选 generic-argument-clause 的用途。在这些成员之后被禁止:

S().property<Int>   // error: '>' is not a postfix unary operator
S().function<Int>(3) // error: cannot explicitly specialize a generic function

那么在什么情况下我们可以使用generic-argument-clauses?

模块成员表达式的通用参数子句

generic-argument-clause 可能只有 模块 (SomeModule.SomeGeneric<SomeType>()) 的用例。来自 Language Reference - Expressions:

Explicit Member Expression

An explicit member expression allows access to the members of a named type, a tuple, or a module. It consists of a period (.) between the item and the identifier of its member.

举个例子:

/* Module 'MyLib.a' ... */
class MyClass<T> {
    var foo: T?
}

/* ... elsewhere: explicit member expression to module */
MyLib.MyClass<Int>()

奇怪的是,implicit member expressions 不包含 generic-argument-clause[opt] 的语法,我们可以将其解释为隐式游标,对于显式成员表达式,后者至少不涉及枚举;可能会略微加强这只涉及模块的理论。

Grammar Of A Implicit Member Expression

implicit-member-expression → .­identifier­

模块之外的其他用例?

我不能肯定地说以上仅适用于模块,但我无法在成员表达式的上下文中找到通用参数子句的任何其他用途。

下面是一些讨论性的相关参考摘录;除了模块之外,它可能对其他人对此的调查具有一定的价值。


首先,什么是泛型参数子句?来自 Language Reference - Generic Parameters and Arguments - Generic Argument Clause

A generic argument clause specifies the type arguments of a generic type.

...

The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.

...

The specialized version of the generic Dictionary type, Dictionary<String, Int> is formed by replacing the generic parameters Key: Hashable and Value with the concrete type arguments String and Int.

现在,同一部分以以下语句结束:

As mentioned in Generic Parameter Clause, you don’t use a generic argument clause to specify the type arguments of a generic function or initializer.

我们跳转到 通用参数子句 部分并阅读:

... In contrast with generic types, you don’t specify a generic argument clause when you use a generic function or initializer. The type arguments are instead inferred from the type of the arguments passed to the function or initializer.

所以对于这个问题的主题,我们应该关注成员表达式和(.右侧)[=59的组合=]泛型;不是通用函数。但是在什么情况下——除了模块——我们可以将这两个和通用参数子句结合起来?可能首先想到的是 enum 和关联的泛型类型:

enum Bar<T> {
    case One(T)
    case Two
}

var foo = Bar.One(1)
print(foo.dynamicType) // Bar<Int>

但这是类型推断,而不是泛型参数子句。


综上所述,我只能将模块视为成员表达式上下文中语法generic-argument-clause的用例。