|
1 |
| -<!-- |
2 |
| -要翻译的文件:https://github.com/SwiftGGTeam/the-swift-programming-language-in-chinese/blob/swift-6-beta-translation/swift-6-beta.docc/LanguageGuide/OpaqueTypes.md |
3 |
| -Swift 文档源文件地址:https://docs.swift.org/swift-book/documentation/the-swift-programming-language/opaquetypes |
4 |
| -翻译估计用时:⭐️⭐️⭐️⭐️⭐️ |
5 |
| ---> |
| 1 | +# 不透明类型和封装协议类型 |
6 | 2 |
|
7 |
| -# Opaque and Boxed Protocol Types |
| 3 | +隐藏关于值类型的实现细节。 |
8 | 4 |
|
9 |
| -Hide implementation details about a value's type. |
| 5 | +Swift 提供了两种隐藏值类型细节的方法:不透明类型和封装协议类型。 |
| 6 | +在模块与调用该模块的代码之间的边界上隐藏类型信息是有用的,因为这样返回值的底层类型可以保持为私有的可访问状态。 |
10 | 7 |
|
11 |
| -Swift provides two ways to hide details about a value's type: |
12 |
| -opaque types and boxed protocol types. |
13 |
| -Hiding type information |
14 |
| -is useful at boundaries between |
15 |
| -a module and code that calls into the module, |
16 |
| -because the underlying type of the return value can remain private. |
| 8 | +返回不透明类型的函数或方法隐藏了其返回值的类型信息。 |
| 9 | +相较于提供一个具体的类型作为函数的返回类型,它会根据它支持的协议来描述它返回值。 |
| 10 | +不透明类型会保留类型的身份标识 —— 编译器可以访问类型信息,但模块的调用端则无法访问。 |
17 | 11 |
|
18 |
| -A function or method that returns an opaque type |
19 |
| -hides its return value's type information. |
20 |
| -Instead of providing a concrete type as the function's return type, |
21 |
| -the return value is described in terms of the protocols it supports. |
22 |
| -Opaque types preserve type identity --- |
23 |
| -the compiler has access to the type information, |
24 |
| -but clients of the module don't. |
| 12 | +封装协议类型可以存储符合给定协议的任何类型的实例。 |
| 13 | +封装协议类型不保留类型的身份标识 —— 值的具体类型在运行时才会被知道,并且随着不同的值被存储其中,它的具体类型可能会发生变化。 |
25 | 14 |
|
26 |
| -A boxed protocol type can store an instance of any type |
27 |
| -that conforms to the given protocol. |
28 |
| -Boxed protocol types don't preserve type identity --- |
29 |
| -the value's specific type isn't known until runtime, |
30 |
| -and it can change over time as different values are stored. |
| 15 | +## 不透明类型所解决的问题 |
31 | 16 |
|
32 |
| -## The Problem That Opaque Types Solve |
33 |
| - |
34 |
| -For example, |
35 |
| -suppose you're writing a module that draws ASCII art shapes. |
36 |
| -The basic characteristic of an ASCII art shape |
37 |
| -is a `draw()` function that returns the string representation of that shape, |
38 |
| -which you can use as the requirement for the `Shape` protocol: |
| 17 | +举个例子,假设你正在编写一个用 ASCII 字符绘制几何形状的程序模块。 |
| 18 | +每个几何形状结构体的基本特征是有一个 `draw()` 函数,该函数返回表示那个几何形状的字符串,这样你就可以把这个基本特征作为 `Shape` 协议的要求之一: |
39 | 19 |
|
40 | 20 | ```swift
|
41 | 21 | protocol Shape {
|
@@ -85,11 +65,8 @@ print(smallTriangle.draw())
|
85 | 65 | ```
|
86 | 66 | -->
|
87 | 67 |
|
88 |
| -You could use generics to implement operations like flipping a shape vertically, |
89 |
| -as shown in the code below. |
90 |
| -However, there's an important limitation to this approach: |
91 |
| -The flipped result exposes the exact generic types |
92 |
| -that were used to create it. |
| 68 | +如下面的代码所示,你可以使用泛型来实现像垂直翻转某个几何转形状这样的操作。 |
| 69 | +然而,这种方法有一个重要的局限性:翻转后的结果会暴露用于创建该结果的确切的泛型类型。 |
93 | 70 |
|
94 | 71 | ```swift
|
95 | 72 | struct FlippedShape<T: Shape>: Shape {
|
@@ -125,10 +102,7 @@ print(flippedTriangle.draw())
|
125 | 102 | ```
|
126 | 103 | -->
|
127 | 104 |
|
128 |
| -This approach to defining a `JoinedShape<T: Shape, U: Shape>` structure |
129 |
| -that joins two shapes together vertically, like the code below shows, |
130 |
| -results in types like `JoinedShape<Triangle, FlippedShape<Triangle>>` |
131 |
| -from joining a triangle with a flipped triangle. |
| 105 | +又比如下面这样的代码,这种方法定义了一个 `JoinedShape<T: Shape, U: Shape>` 结构体将两个形状垂直连接在一起,如果把一个三角形与一个翻转过的三角形连接在一起,就产生了像 `JoinedShape<Triangle, FlippedShape<Triangle>>` 这样的类型。 |
132 | 106 |
|
133 | 107 | ```swift
|
134 | 108 | struct JoinedShape<T: Shape, U: Shape>: Shape {
|
@@ -170,21 +144,10 @@ print(joinedTriangles.draw())
|
170 | 144 | ```
|
171 | 145 | -->
|
172 | 146 |
|
173 |
| -Exposing detailed information about the creation of a shape |
174 |
| -allows types that aren't meant to be |
175 |
| -part of the ASCII art module's public interface |
176 |
| -to leak out because of the need to state the full return type. |
177 |
| -The code inside the module |
178 |
| -could build up the same shape in a variety of ways, |
179 |
| -and other code outside the module |
180 |
| -that uses the shape shouldn't have to account for |
181 |
| -the implementation details about the list of transformations. |
182 |
| -Wrapper types like `JoinedShape` and `FlippedShape` |
183 |
| -don't matter to the module's users, |
184 |
| -and they shouldn't be visible. |
185 |
| -The module's public interface |
186 |
| -consists of operations like joining and flipping a shape, |
187 |
| -and those operations return another `Shape` value. |
| 147 | +因为我们总是需要声明完整的返回类型,所以暴露关于形状创建的详细信息会导致类型泄露,这些泄漏的类型本不应成为绘制几何形状程序模块公开接口的一部分。 |
| 148 | +模块内部的代码可以以多种不同的方式构建相同的形状,而其他使用该形状的模块外部代码不应需要考虑关于变换几何形状的具体实现细节。 |
| 149 | +像 `JoinedShape` 和 `FlippedShape` 这样的包装类型(wrapper types)对模块的用户来说并不重要,它们不应被可见。 |
| 150 | +该模块的公开接口包括连接和翻转形状等操作,这些操作会返回另一个 `Shape` 值。 |
188 | 151 |
|
189 | 152 | ## Returning an Opaque Type
|
190 | 153 |
|
|
0 commit comments