Skip to content

Commit 674b577

Browse files
committed
wip: Declaration Modifiers
1 parent 8cd1f6b commit 674b577

File tree

1 file changed

+18
-112
lines changed

1 file changed

+18
-112
lines changed

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

Lines changed: 18 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,58 +2755,23 @@ Swift 定义了许多优先级组,以配合 Swift 标准库提供的运算符
27552755
> *优先级组名称**优先级组名称* | *优先级组名称* **`,`** *优先级组名称*
27562756
> *优先级组名称**标识符*
27572757
2758-
## Declaration Modifiers
2759-
2760-
*Declaration modifiers* are keywords or context-sensitive keywords that modify the behavior
2761-
or meaning of a declaration. You specify a declaration modifier by writing the appropriate
2762-
keyword or context-sensitive keyword between a declaration's attributes (if any) and the keyword
2763-
that introduces the declaration.
2764-
2765-
- term `class`:
2766-
Apply this modifier to a member of a class
2767-
to indicate that the member is a member of the class itself,
2768-
rather than a member of instances of the class.
2769-
Members of a superclass that have this modifier
2770-
and don't have the `final` modifier
2771-
can be overridden by subclasses.
2772-
2773-
- term `dynamic`:
2774-
Apply this modifier to any member of a class that can be represented by Objective-C.
2775-
When you mark a member declaration with the `dynamic` modifier,
2776-
access to that member is always dynamically dispatched using the Objective-C runtime.
2777-
Access to that member is never inlined or devirtualized by the compiler.
2778-
2779-
Because declarations marked with the `dynamic` modifier are dispatched
2780-
using the Objective-C runtime, they must be marked with the
2781-
`objc` attribute.
2782-
2783-
- term `final`:
2784-
Apply this modifier to a class or to a property, method,
2785-
or subscript member of a class. It's applied to a class to indicate that the class
2786-
can't be subclassed. It's applied to a property, method, or subscript of a class
2787-
to indicate that a class member can't be overridden in any subclass.
2788-
For an example of how to use the `final` attribute,
2789-
see <doc:Inheritance#Preventing-Overrides>.
2790-
2791-
- term `lazy`:
2792-
Apply this modifier to a stored variable property of a class or structure
2793-
to indicate that the property's initial value is calculated and stored at most
2794-
once, when the property is first accessed.
2795-
For an example of how to use the `lazy` modifier,
2796-
see <doc:Properties#Lazy-Stored-Properties>.
2797-
2798-
- term `optional`:
2799-
Apply this modifier to a protocol's property, method,
2800-
or subscript members to indicate that a conforming type isn't required
2801-
to implement those members.
2802-
2803-
You can apply the `optional` modifier only to protocols that are marked
2804-
with the `objc` attribute. As a result, only class types can adopt and conform
2805-
to a protocol that contains optional member requirements.
2806-
For more information about how to use the `optional` modifier
2807-
and for guidance about how to access optional protocol members ---
2808-
for example, when you're not sure whether a conforming type implements them ---
2809-
see <doc:Protocols#Optional-Protocol-Requirements>.
2758+
## 声明修饰符
2759+
2760+
*声明修饰符* 是修饰声明行为或含义的关键字或上下文敏感关键字。你可以通过在声明的属性(如果有的话)和引入声明的关键字之间,写上适当的关键字或上下文敏感关键字来指定声明修饰符。
2761+
2762+
- `class`:将此修饰符应用于类的成员,以指示该成员是类本身的成员,而不是类实例的成员。具有此修饰符且没有 `final` 修饰符的超类成员可以被子类重写。
2763+
2764+
- `dynamic`:将此修饰符应用于可以用 Objective-C 表示的类的任何成员。当您使用 `dynamic` 修饰符标记成员声明时,对该成员的访问始终通过 Objective-C 运行时动态分派。对该成员的访问永远不会被编译器内联或去虚拟化。
2765+
2766+
因为带有 `dynamic` 修饰符的声明是通过 Objective-C 运行时进行调度的,因此它们必须标记为 `objc` 属性。
2767+
2768+
- `final`:将此修饰符应用于类或类的属性、方法或下标成员。它应用于类以指示该类不能被子类化。它应用于类的属性、方法或下标,以指示类成员在任何子类中不能被重写。有关如何使用 `final` 属性的示例,请参见 <doc:Inheritance#Preventing-Overrides>
2769+
2770+
- `lazy`:将此修饰符应用于类或结构体的存储变量属性,以指示该属性的初始值在第一次访问该属性时最多计算并存储一次。有关如何使用 `lazy` 修饰符的示例,请参见 <doc:Properties#Lazy-Stored-Properties>
2771+
2772+
- `optional`:将此修饰符应用于协议的属性、方法或下标成员,表示实现该协议的类型不必实现这些成员。
2773+
2774+
你只能将 `optional` 修饰符应用于带有 `objc` 属性的协议。因此,只有类类型可以采用并遵循包含可选成员要求的协议。有关如何使用 `optional` 修饰符的更多信息,以及在不确定遵循类型是否实现了这些成员时如何访问可选协议成员的指导,请参见 <doc:Protocols#Optional-Protocol-Requirements>
28102775

28112776
<!--
28122777
TODO: Currently, you can't check for an optional initializer,
@@ -2816,66 +2781,7 @@ that introduces the declaration.
28162781
properly for optional initializer requirements.
28172782
-->
28182783

2819-
- term `required`:
2820-
Apply this modifier to a designated or convenience initializer
2821-
of a class to indicate that every subclass must implement that initializer.
2822-
The subclass's implementation of that initializer
2823-
must also be marked with the `required` modifier.
2824-
2825-
- term `static`:
2826-
Apply this modifier to a member of a structure, class, enumeration, or protocol
2827-
to indicate that the member is a member of the type,
2828-
rather than a member of instances of that type.
2829-
In the scope of a class declaration,
2830-
writing the `static` modifier on a member declaration
2831-
has the same effect as writing the `class` and `final` modifiers
2832-
on that member declaration.
2833-
However, constant type properties of a class are an exception:
2834-
`static` has its normal, nonclass meaning there
2835-
because you can't write `class` or `final` on those declarations.
2836-
2837-
- term `unowned`:
2838-
Apply this modifier to a stored variable, constant, or stored property
2839-
to indicate that the variable or property has an unowned reference
2840-
to the object stored as its value.
2841-
If you try to access the variable or property
2842-
after the object has been deallocated,
2843-
a runtime error is raised.
2844-
Like a weak reference,
2845-
the type of the property or value must be a class type;
2846-
unlike a weak reference,
2847-
the type is non-optional.
2848-
For an example and more information about the `unowned` modifier,
2849-
see <doc:AutomaticReferenceCounting#Unowned-References>.
2850-
2851-
- term `unowned(safe)`:
2852-
An explicit spelling of `unowned`.
2853-
2854-
- term `unowned(unsafe)`:
2855-
Apply this modifier to a stored variable, constant, or stored property
2856-
to indicate that the variable or property has an unowned reference
2857-
to the object stored as its value.
2858-
If you try to access the variable or property
2859-
after the object has been deallocated,
2860-
you'll access the memory at the location where the object used to be,
2861-
which is a memory-unsafe operation.
2862-
Like a weak reference,
2863-
the type of the property or value must be a class type;
2864-
unlike a weak reference,
2865-
the type is non-optional.
2866-
For an example and more information about the `unowned` modifier,
2867-
see <doc:AutomaticReferenceCounting#Unowned-References>.
2868-
2869-
- term `weak`:
2870-
Apply this modifier to a stored variable or stored variable property
2871-
to indicate that the variable or property has a weak reference to the
2872-
object stored as its value. The type of the variable or property
2873-
must be an optional class type.
2874-
If you access the variable or property
2875-
after the object has been deallocated,
2876-
its value is `nil`.
2877-
For an example and more information about the `weak` modifier,
2878-
see <doc:AutomaticReferenceCounting#Weak-References>.
2784+
- `required`:将此修饰符应用于类的指定或便利构造器,以指示每个子类必须实现该构造器。子类对该构造器的实现也必须标记为 `required` 修饰符。
28792785

28802786
### Access Control Levels
28812787

0 commit comments

Comments
 (0)