gpt4 book ai didi

Swift 文法显式成员表达式

转载 作者:可可西里 更新时间:2023-11-01 01:37:21 25 4
gpt4 key购买 nike

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

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-clause

最佳答案

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

有可能 generic-argument-clause只有 modules ( 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­

模块之外的其他用例?

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

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


首先,什么是泛型参数子句?来自 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.

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

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

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

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


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

关于Swift 文法显式成员表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com