@@ -599,13 +599,13 @@ can enforce its safe use.
599
599
We've considered two possible syntaxes for this::
600
600
601
601
@available(1.1)
602
- extension MyStruct : SomeProto {…}
602
+ extension Wand : MagicType {…}
603
603
604
604
and
605
605
606
606
::
607
607
608
- extension MyStruct : @available(1.1) SomeProto {…}
608
+ extension Wand : @available(1.1) MagicType {…}
609
609
610
610
The former requires fewer changes to the language grammar, but the latter could
611
611
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
1465
1465
::
1466
1466
1467
1467
// Library, version 1
1468
- class Base {}
1469
- protocol P {}
1468
+ class Elf {}
1469
+ protocol Summonable {}
1470
1470
1471
1471
::
1472
1472
1473
1473
// Client, version 1
1474
- class Subclass : Base, P {}
1474
+ class ShoemakingElf : Elf, Summonable {}
1475
1475
1476
1476
::
1477
1477
1478
1478
// Library, version 2
1479
1479
@available(2.0)
1480
- extension Base : P {}
1480
+ extension Elf : Summonable {}
1481
1481
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 ``).
1485
1485
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).
1492
1492
1493
1493
One solution is to disallow adding a conformance for an existing protocol to a
1494
1494
publicly-subclassable class.
@@ -1500,34 +1500,34 @@ Recompiling changes a protocol's implementation
1500
1500
::
1501
1501
1502
1502
// 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) {}
1506
1506
1507
1507
::
1508
1508
1509
1509
// Client, version 1
1510
- struct S : P, Q {}
1511
- use(S ())
1510
+ struct Amulet : MagicType, Wearable {}
1511
+ use(Amulet ())
1512
1512
1513
1513
::
1514
1514
1515
1515
// Library, version 2
1516
- protocol P {
1516
+ protocol MagicType {
1517
1517
@available(2.0)
1518
- func foo () { print("default ") }
1518
+ func equip () { print("Equipped. ") }
1519
1519
}
1520
1520
1521
- extension P where Self: Q {
1521
+ extension Wearable where Self: MagicType {
1522
1522
@available(2.0)
1523
- func foo () { print("constrained ") }
1523
+ func equip () { print("You put it on. ") }
1524
1524
}
1525
1525
1526
- func use<T: P>(value : T) { value.foo () }
1526
+ func use<T: MagicType>(item : T) { item.equip () }
1527
1527
1528
1528
Before the client is recompiled, the implementation of ``foo() `` used for ``S ``
1529
1529
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
1531
1531
implementation being considered a "better" match for the protocol requirement,
1532
1532
thus changing the behavior of the program.
1533
1533
0 commit comments