6
6
:Author: Jordan Rose
7
7
:Author: John McCall
8
8
9
+ .. note ::
10
+
11
+ This document uses some Sphinx-specific features which are not available on
12
+ GitHub. For proper rendering, download and build the docs yourself. Jordan
13
+ Rose also posts occasional snapshots at
14
+ https://jrose-apple.github.io/swift-library-evolution/.
15
+
9
16
One of Swift's primary design goals is to allow efficient execution of code
10
17
without sacrificing load-time abstraction of implementation.
11
18
@@ -167,7 +174,7 @@ document.
167
174
Publishing Versioned API
168
175
========================
169
176
170
- A library's API is already marked with the ``public `` attribute , but if a
177
+ A library's API is already marked with the ``public `` modifier , but if a
171
178
client wants to work with multiple releases of the library, the API needs
172
179
versioning information as well. A *versioned entity * represents anything with a
173
180
runtime presence that a client may rely on; its version records when the entity
@@ -182,9 +189,12 @@ version of the library where the entity may be used.
182
189
See `New Conformances `_, below.
183
190
184
191
In a versioned library, any top-level public entity from the list above may not
185
- be made ``public `` without an appropriate version. A public entity declared
186
- within a versioned type (or an extension of a versioned type) will default to
187
- having the same version as the type.
192
+ be made ``public `` (or ``open ``) without an appropriate version. A public
193
+ entity declared within a versioned type (or an extension of a versioned type)
194
+ will default to having the same version as the type.
195
+
196
+ In this document, the term "public" includes classes and members marked
197
+ ``open ``.
188
198
189
199
Code within a library may generally use all other entities declared within the
190
200
library (barring their own availability checks), since the entire library is
@@ -286,8 +296,6 @@ The following changes are permitted:
286
296
- Adding a default argument expression to a parameter.
287
297
- Changing or removing a default argument is a `binary-compatible
288
298
source-breaking change `.
289
- - The ``@noreturn `` attribute may be added to a function. ``@noreturn `` is a
290
- `versioned attribute `.
291
299
- The ``@discardableResult `` and ``@warn_unqualified_access `` attributes may
292
300
be added to a function without any additional versioning information.
293
301
@@ -299,7 +307,6 @@ No other changes are permitted; the following are particularly of note:
299
307
- A versioned function may not add, remove, or reorder parameters, whether or
300
308
not they have default arguments.
301
309
- A versioned function that throws may not become non-throwing or vice versa.
302
- - ``@noreturn `` may not be removed from a function.
303
310
- The ``@noescape `` attribute may not be added to or removed from a parameter.
304
311
It is not a `versioned attribute ` and so there is no way to guarantee that it
305
312
is safe when a client deploys against older versions of the library.
@@ -952,16 +959,17 @@ Omitted from this list is the free addition of new members. Here classes are a
952
959
little more restrictive than structs; they only allow the following changes:
953
960
954
961
- Adding a new convenience initializer.
955
- - Adding a new designated initializer, if the class is not publicly
956
- subclassable.
962
+ - Adding a new designated initializer, if the class is not ``open ``.
957
963
- Adding a deinitializer.
958
964
- Adding new, non-overriding method, subscript, or property.
959
- - Adding a new overriding member, though if the class is publicly-subclassable
960
- the type of the member may not deviate from the member it overrides.
961
- Changing the type could be incompatible with existing overrides in subclasses.
965
+ - Adding a new overriding member, though if the class is `` open `` the type of
966
+ the member may not deviate from the member it overrides. Changing the type
967
+ could be incompatible with existing overrides in subclasses.
962
968
963
969
Finally, classes allow the following changes that do not apply to structs:
964
970
971
+ - A class may be marked ``open `` if it is not already marked ``final ``.
972
+ - A class may be marked ``final `` if it is not already marked ``open ``.
965
973
- Removing an explicit deinitializer. (A class with no declared deinitializer
966
974
effectively has an implicit deinitializer.)
967
975
- "Moving" a method, subscript, or property up to its superclass. The
@@ -972,8 +980,10 @@ Finally, classes allow the following changes that do not apply to structs:
972
980
removed as long as the generic parameters, formal parameters, and return type
973
981
*exactly * match the overridden declaration. Any existing callers should
974
982
automatically use the superclass implementation.
975
- - ``@noreturn `` may be only added to a method if it is not publicly
976
- overridable.
983
+ - Within an ``open `` class, any public method, subscript, or property may be
984
+ marked ``open `` if it is not already marked ``final ``.
985
+ - Any public method, subscript, or property may be marked ``final `` if it is not
986
+ already marked ``open ``.
977
987
- ``@IBOutlet ``, ``@IBAction ``, and ``@IBInspectable `` may be added to a member
978
988
without providing any extra version information. Removing any of these is
979
989
a `binary-compatible source-breaking change ` if the member remains ``@objc ``,
@@ -996,14 +1006,21 @@ Finally, classes allow the following changes that do not apply to structs:
996
1006
NSCollectionViewItem be a subclass of NSResponder or not? How would the
997
1007
compiler be able to enforce this?
998
1008
1009
+ .. admonition :: TODO
1010
+
1011
+ Both ``final `` and ``open `` may be applied to a declaration after it has
1012
+ been made public. However, these need to be treated as
1013
+ `versioned attributes <versioned attribute> `. It's not clear what syntax
1014
+ should be used for this.
1015
+
999
1016
.. _NSCollectionViewItem : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSCollectionViewItem_Class/index.html
1000
1017
1001
1018
Other than those detailed above, no other changes to a class or its members
1002
1019
are permitted. In particular:
1003
1020
1004
- - ``final `` may not be added to * or * removed from a class or any of its members.
1005
- The presence of ``final `` enables optimization; its absence means there may
1006
- be subclasses/overrides that would be broken by the change.
1021
+ - ``open `` may not be removed from a class or its members.
1022
+ - ``final `` may not be removed from a class or its members. (The presence of
1023
+ `` final `` enables optimization.)
1007
1024
- ``dynamic `` may not be added to *or * removed from any members. Existing
1008
1025
clients would not know to invoke the member dynamically.
1009
1026
- A ``final `` override of a member may *not * be removed, even if the type
@@ -1013,9 +1030,6 @@ are permitted. In particular:
1013
1030
any existing members.
1014
1031
- ``@NSManaged `` may not be added to or removed from any existing members.
1015
1032
1016
- .. note :: These restrictions tie in with the ongoing discussions about
1017
- "``final ``-by-default" and "non-publicly-subclassable-by-default".
1018
-
1019
1033
.. admonition :: TODO
1020
1034
1021
1035
The ``@NSManaged `` attribute as it is in Swift 2 exposes implementation
@@ -1026,10 +1040,10 @@ are permitted. In particular:
1026
1040
Initializers
1027
1041
------------
1028
1042
1029
- New designated initializers may not be added to a publicly-subclassable class.
1030
- This would change the inheritance of convenience initializers, which existing
1031
- subclasses may depend on. A publicly-subclassable class also may not change
1032
- a convenience initializer into a designated initializer or vice versa.
1043
+ New designated initializers may not be added to an `` open `` class. This would
1044
+ change the inheritance of convenience initializers, which existing subclasses
1045
+ may depend on. An `` open `` class also may not change a convenience initializer
1046
+ into a designated initializer or vice versa.
1033
1047
1034
1048
A new ``required `` initializer may be added to a class only if it is a
1035
1049
convenience initializer; that initializer may only call existing ``required ``
@@ -1054,9 +1068,6 @@ top-level functions, but the potential for overrides complicates things a little
1054
1068
- Adding a default argument expression to a parameter.
1055
1069
- Changing or removing a default argument is a `binary-compatible
1056
1070
source-breaking change `.
1057
- - The ``@noreturn `` attribute may be added to a public method only if it is
1058
- ``final `` or the class is not publicly subclassable. ``@noreturn `` is a
1059
- `versioned attribute `.
1060
1071
- The ``@discardableResult `` and ``@warn_unqualified_access `` attributes may
1061
1072
be added to a method without any additional versioning information.
1062
1073
@@ -1069,9 +1080,6 @@ If an inlineable method is overridden, the overriding method does not need to
1069
1080
also be inlineable. Clients may only inline a method when they can devirtualize
1070
1081
the call. (This does permit speculative devirtualization.)
1071
1082
1072
- Any method that overrides a ``@noreturn `` method must also be marked
1073
- ``@noreturn ``.
1074
-
1075
1083
1076
1084
Properties
1077
1085
----------
@@ -1080,8 +1088,7 @@ Class and instance properties allow *most* of the modifications permitted for
1080
1088
struct properties, but the potential for overrides complicates things a little.
1081
1089
Variable properties (those declared with ``var ``) allow the following changes:
1082
1090
1083
- - Adding (but not removing) a computed setter to a ``final `` property or a
1084
- property in a non-publicly-subclassable class.
1091
+ - Adding (but not removing) a computed setter to a non-``open `` property.
1085
1092
- Adding or removing a non-public, non-versioned setter.
1086
1093
- Changing from a stored property to a computed property, or vice versa, as
1087
1094
long as a previously versioned setter is not removed.
@@ -1094,7 +1101,7 @@ Variable properties (those declared with ``var``) allow the following changes:
1094
1101
- Adding or removing ``unowned `` from a variable.
1095
1102
- Adding or removing ``@NSCopying `` to/from a variable.
1096
1103
1097
- Adding a public setter to a computed property that may be overridden is a
1104
+ Adding a public setter to an `` open `` property is a
1098
1105
`binary-compatible source-breaking change `; any existing overrides will not
1099
1106
know what to do with the setter and will likely not behave correctly.
1100
1107
@@ -1117,12 +1124,12 @@ Subscripts
1117
1124
Subscripts behave much like properties; they inherit the rules of their struct
1118
1125
counterparts with a few small changes:
1119
1126
1120
- - Adding (but not removing) a public setter to a `` final `` subscript or a
1121
- subscript is permitted in a non-publicly-subclassable class .
1127
+ - Adding (but not removing) a public setter to a non-`` open `` subscript is
1128
+ permitted.
1122
1129
- Adding or removing a non-public, non-versioned setter is permitted.
1123
1130
- Changing the body of an accessor is permitted.
1124
1131
1125
- Adding a public setter to a subscript that may be overridden is a
1132
+ Adding a public setter to an `` open `` subscript is a
1126
1133
`binary-compatible source-breaking change `; any existing overrides will not
1127
1134
know what to do with the setter and will likely not behave correctly.
1128
1135
@@ -1245,28 +1252,28 @@ multiple kinds of flexibility.
1245
1252
Versioning Internal Declarations
1246
1253
================================
1247
1254
1248
- The initial discussion on versioning focused on `` public `` APIs, making sure
1255
+ The initial discussion on versioning focused on public APIs, making sure
1249
1256
that a client knows what features they can use when a specific version of a
1250
1257
library is present. Inlineable functions have much the same constraints, except
1251
1258
the inlineable function is the client and the entities being used may not be
1252
- `` public `` .
1259
+ public.
1253
1260
1254
1261
Adding a versioning annotation to an ``internal `` entity promises that the
1255
1262
entity will be available at link time in the containing module's binary. This
1256
1263
makes it safe to refer to such an entity from an inlineable function. If the
1257
- entity is ever made ``public ``, its availability should not be changed; not
1258
- only is it safe for new clients to rely on it, but *existing * clients require
1259
- its presence as well.
1264
+ entity is ever made ``public `` or `` open `` , its availability should not be
1265
+ changed; not only is it safe for new clients to rely on it, but *existing *
1266
+ clients require its presence as well.
1260
1267
1261
1268
.. note ::
1262
1269
1263
1270
Why isn't this a special form of ``public ``? Because we don't want it to
1264
1271
imply everything that ``public `` does, such as requiring overrides to be
1265
1272
``public ``.
1266
1273
1267
- Because a versioned class member may eventually be made ``public ``, it must be
1268
- assumed that new overrides may eventually appear from outside the module unless
1269
- the member is marked ``final `` or the class is not publicly subclassable .
1274
+ Because a versioned class member may eventually be made ``open ``, it must be
1275
+ assumed that new overrides may eventually appear from outside the module if the
1276
+ class is marked ``open `` unless the member is marked `` final `` .
1270
1277
1271
1278
Non-public conformances are never considered versioned, even if both the
1272
1279
conforming type and the protocol are versioned. A conformance is considered
@@ -1611,8 +1618,8 @@ of ``Summonable`` from the base class, but even that may not be possible if
1611
1618
there are incompatible associated types involved (because changing a member
1612
1619
typealias is not a safe change).
1613
1620
1614
- One solution is to disallow adding a conformance for an existing protocol to a
1615
- publicly-subclassable class.
1621
+ One solution is to disallow adding a conformance for an existing protocol to an
1622
+ `` open `` class.
1616
1623
1617
1624
1618
1625
Recompiling changes a protocol's implementation
@@ -1689,10 +1696,10 @@ document that affect language semantics:
1689
1696
1690
1697
- `SE-0030 Property Behaviors `_
1691
1698
- (draft) `Overridable methods in extensions `_
1692
- - (planned) Making classes "sealed" by default
1699
+ - ` SE-0117 Allow Allow distinguishing between public access and public overridability < SE-0117 >`_
1693
1700
- (planned) Restricting retroactive modeling (protocol conformances for types you don't own)
1694
1701
- (planned) Default implementations in protocols
1695
- - (planned) Generalized existentials (values of protocol type)
1702
+ - (planned) ` Generalized existentials (values of protocol type) < Generics >`_
1696
1703
- (planned) Open and closed enums
1697
1704
- (planned) Removing the "constant" guarantee for 'let' across module boundaries
1698
1705
- (planned) Syntax for declaring "versioned" entities and their features
@@ -1705,6 +1712,8 @@ document that affect language semantics:
1705
1712
1706
1713
.. _SE-0030 Property Behaviors : https://github.com/apple/swift-evolution/blob/master/proposals/0030-property-behavior-decls.md
1707
1714
.. _Overridable methods in extensions : https://github.com/jrose-apple/swift-evolution/blob/overridable-members-in-extensions/proposals/nnnn-overridable-members-in-extensions.md
1715
+ .. _SE-0117 : https://github.com/apple/swift-evolution/blob/master/proposals/0117-non-public-subclassable-by-default.md
1716
+ .. _Generics : https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generalized-existentials
1708
1717
1709
1718
This does not mean all of these proposals need to be accepted, only that their
1710
1719
acceptance or rejection will affect this document.
@@ -1723,7 +1732,7 @@ Glossary
1723
1732
API
1724
1733
An `entity ` in a library that a `client ` may use, or the collection of all
1725
1734
such entities in a library. (If contrasting with `SPI `, only those entities
1726
- that are available to arbitrary clients.) Marked ``public `` in
1735
+ that are available to arbitrary clients.) Marked ``public `` or `` open `` in
1727
1736
Swift. Stands for "Application Programming Interface".
1728
1737
1729
1738
availability context
0 commit comments