Skip to content

Commit 552e849

Browse files
committed
wip: Overridden Requirements Aren't Used in Some Generic Contexts
1 parent b0b8735 commit 552e849

File tree

1 file changed

+5
-25
lines changed

1 file changed

+5
-25
lines changed

swift-6-beta.docc/ReferenceManual/Declarations.md

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,15 +2198,9 @@ extension <#type name#>: <#adopted protocols#> where <#requirements#> {
21982198

21992199
您可以扩展一个泛型类型以有条件地遵循一个协议,从而使该类型的实例仅在满足某些要求时遵循该协议。您通过在扩展声明中包含*要求*来添加对协议的条件遵循。
22002200

2201-
#### Overridden Requirements Aren't Used in Some Generic Contexts
2201+
#### 重写的要求在某些泛型上下文中不会被使用
22022202

2203-
In some generic contexts,
2204-
types that get behavior from conditional conformance to a protocol
2205-
don't always use the specialized implementations
2206-
of that protocol's requirements.
2207-
To illustrate this behavior,
2208-
the following example defines two protocols
2209-
and a generic type that conditionally conforms to both protocols.
2203+
在某些泛型上下文中,通过条件遵循协议而获得行为的类型,并不总是使用该协议要求的特定实现。为了说明这种行为,以下示例定义了两个协议和一个有条件地遵循这两个协议的泛型类型。
22102204

22112205
<!--
22122206
This test needs to be compiled so that it will recognize Pair's
@@ -2300,14 +2294,7 @@ extension String: TitledLoggable {
23002294
```
23012295
-->
23022296

2303-
The `Pair` structure conforms to `Loggable` and `TitledLoggable`
2304-
whenever its generic type conforms to `Loggable` or `TitledLoggable`, respectively.
2305-
In the example below,
2306-
`oneAndTwo` is an instance of `Pair<String>`,
2307-
which conforms to `TitledLoggable`
2308-
because `String` conforms to `TitledLoggable`.
2309-
When the `log()` method is called on `oneAndTwo` directly,
2310-
the specialized version containing the title string is used.
2297+
`Pair` 结构在其泛型类型分别遵循 `Loggable``TitledLoggable` 时,也会相应地遵循 `Loggable``TitledLoggable`。在下面的示例中,`oneAndTwo``Pair<String>` 的一个实例,由于 `String` 遵循 `TitledLoggable`,因此 `oneAndTwo` 也遵循 `TitledLoggable`。当直接调用 `oneAndTwo``log()` 方法时,将使用包含标题字符串的特定版本。
23112298

23122299
```swift
23132300
let oneAndTwo = Pair(first: "one", second: "two")
@@ -2325,13 +2312,7 @@ oneAndTwo.log()
23252312
```
23262313
-->
23272314

2328-
However, when `oneAndTwo` is used in a generic context
2329-
or as an instance of the `Loggable` protocol,
2330-
the specialized version isn't used.
2331-
Swift picks which implementation of `log()` to call
2332-
by consulting only the minimum requirements that `Pair` needs to conform to `Loggable`.
2333-
For this reason,
2334-
the default implementation provided by the `Loggable` protocol is used instead.
2315+
然而,当在泛型上下文中使用 `oneAndTwo` 或将其作为 `Loggable` 协议的一个实例时,特定的实现版本不会被使用。`Swift` 在选择调用哪个 `log()` 实现时,只参考 `Pair` 遵循 `Loggable` 所需的最低要求。因此,使用的是 `Loggable` 协议提供的默认实现。
23352316

23362317
```swift
23372318
func doSomething<T: Loggable>(with x: T) {
@@ -2353,8 +2334,7 @@ doSomething(with: oneAndTwo)
23532334
```
23542335
-->
23552336

2356-
When `log()` is called on the instance that's passed to `doSomething(_:)`,
2357-
the customized title is omitted from the logged string.
2337+
当在传递给 `doSomething(_:)` 的实例上调用 `log()` 时,自定义标题会从日志字符串中省略。
23582338

23592339
### Protocol Conformance Must Not Be Redundant
23602340

0 commit comments

Comments
 (0)