Skip to content

Commit 3737ce7

Browse files
committed
[docs] LibraryEvolution: Make code snippets more concrete.
...by giving all the types silly magic-themed names, rather than just 'Base' or 'SomeProto'. That makes a (mostly) consistent theme throughout the document, and hopefully makes it easier to have a mental model about what various changes mean.
1 parent cfff55e commit 3737ce7

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

docs/LibraryEvolution.rst

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,13 @@ can enforce its safe use.
599599
We've considered two possible syntaxes for this::
600600

601601
@available(1.1)
602-
extension MyStruct : SomeProto {…}
602+
extension Wand : MagicType {…}
603603

604604
and
605605

606606
::
607607

608-
extension MyStruct : @available(1.1) SomeProto {…}
608+
extension Wand : @available(1.1) MagicType {…}
609609

610610
The former requires fewer changes to the language grammar, but the latter could
611611
also be used on the declaration of the type itself (i.e. the ``struct``
@@ -1465,30 +1465,30 @@ Subclass and base both conform to protocol
14651465
::
14661466

14671467
// Library, version 1
1468-
class Base {}
1469-
protocol P {}
1468+
class Elf {}
1469+
protocol Summonable {}
14701470

14711471
::
14721472

14731473
// Client, version 1
1474-
class Subclass : Base, P {}
1474+
class ShoemakingElf : Elf, Summonable {}
14751475

14761476
::
14771477

14781478
// Library, version 2
14791479
@available(2.0)
1480-
extension Base : P {}
1480+
extension Elf : Summonable {}
14811481

1482-
Now ``Subclass`` conforms to ``P`` two different ways, which may be
1483-
incompatible (especially if ``P`` has associated types or requirements
1484-
involving ``Self``).
1482+
Now ``ShoemakingElf`` conforms to ``Summonable`` in two different ways, which
1483+
may be incompatible (especially if ``Summonable`` had associated types or
1484+
requirements involving ``Self``).
14851485

1486-
Additionally, the client can't even remove ``Subclass``'s conformance to ``P``,
1487-
because it may itself be a library with other code depending on it. We could
1488-
fix that with an annotation to explicitly inherent the conformance of ``P``
1489-
from the base class, but even that may not be possible if there are
1490-
incompatible associated types involved (because changing a member typealias is
1491-
not a safe change).
1486+
Additionally, the client can't even remove ``ShoemakingElf``'s conformance to
1487+
``Summonable``, because it may itself be a library with other code depending on
1488+
it. We could fix that with an annotation to explicitly inherent the conformance
1489+
of ``Summonable`` from the base class, but even that may not be possible if
1490+
there are incompatible associated types involved (because changing a member
1491+
typealias is not a safe change).
14921492

14931493
One solution is to disallow adding a conformance for an existing protocol to a
14941494
publicly-subclassable class.
@@ -1500,34 +1500,34 @@ Recompiling changes a protocol's implementation
15001500
::
15011501

15021502
// Library, version 1
1503-
protocol P {}
1504-
protocol Q {}
1505-
func use<T: P>(value: T) {}
1503+
protocol MagicType {}
1504+
protocol Wearable {}
1505+
func use<T: MagicType>(item: T) {}
15061506

15071507
::
15081508

15091509
// Client, version 1
1510-
struct S : P, Q {}
1511-
use(S())
1510+
struct Amulet : MagicType, Wearable {}
1511+
use(Amulet())
15121512

15131513
::
15141514

15151515
// Library, version 2
1516-
protocol P {
1516+
protocol MagicType {
15171517
@available(2.0)
1518-
func foo() { print("default") }
1518+
func equip() { print("Equipped.") }
15191519
}
15201520
1521-
extension P where Self: Q {
1521+
extension Wearable where Self: MagicType {
15221522
@available(2.0)
1523-
func foo() { print("constrained") }
1523+
func equip() { print("You put it on.") }
15241524
}
15251525

1526-
func use<T: P>(value: T) { value.foo() }
1526+
func use<T: MagicType>(item: T) { item.equip() }
15271527

15281528
Before the client is recompiled, the implementation of ``foo()`` used for ``S``
15291529
instances can only be the default implementation, i.e. the one that prints
1530-
"default". However, recompiling the client will result in the constrained
1530+
"Equipped". However, recompiling the client will result in the constrained
15311531
implementation being considered a "better" match for the protocol requirement,
15321532
thus changing the behavior of the program.
15331533

0 commit comments

Comments
 (0)