Skip to content

Commit 500310e

Browse files
committed
add is identical methods
1 parent 6bb1157 commit 500310e

File tree

1 file changed

+86
-6
lines changed

1 file changed

+86
-6
lines changed

proposals/NNNN-add-is-identical-methods.md

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,13 @@ When we build and run our SwiftUI app we confirm that we are not computing new `
393393

394394
We propose adding `isIdentical` methods to the following concrete types from Standard Library:
395395
* `String`
396+
* `String.UnicodeScalarView`
397+
* `String.UTF16View`
398+
* `String.UTF8View`
396399
* `Substring`
400+
* `Substring.UnicodeScalarView`
401+
* `Substring.UTF16View`
402+
* `Substring.UTF8View`
397403
* `Array`
398404
* `ArraySlice`
399405
* `ContiguousArray`
@@ -419,6 +425,16 @@ extension String {
419425
/// - If `a.isIdentical(to: b)` and `b.isIdentical(to: c)` are both `true`,
420426
/// then `a.isIdentical(to: c)` is also `true`. (Transitivity)
421427
/// - `a.isIdentical(b)` implies `a == b`
428+
/// - `a == b` does not imply `a.isIdentical(b)`
429+
///
430+
/// Values produced by copying the same value, with no intervening mutations,
431+
/// will compare identical:
432+
///
433+
/// ```swift
434+
/// let d = c
435+
/// print(c.isIdentical(to: d))
436+
/// // Prints true
437+
/// ```
422438
///
423439
/// Comparing strings this way includes comparing (normally) hidden
424440
/// implementation details such as the memory location of any underlying
@@ -431,6 +447,11 @@ extension String {
431447
}
432448
```
433449

450+
The following types will adopt the same semantic guarantees as `String`:
451+
* `String.UnicodeScalarView`
452+
* `String.UTF16View`
453+
* `String.UTF8View`
454+
434455
### `Substring`
435456

436457
```swift
@@ -448,6 +469,16 @@ extension Substring {
448469
/// - If `a.isIdentical(to: b)` and `b.isIdentical(to: c)` are both `true`,
449470
/// then `a.isIdentical(to: c)` is also `true`. (Transitivity)
450471
/// - `a.isIdentical(b)` implies `a == b`
472+
/// - `a == b` does not imply `a.isIdentical(b)`
473+
///
474+
/// Values produced by copying the same value, with no intervening mutations,
475+
/// will compare identical:
476+
///
477+
/// ```swift
478+
/// let d = c
479+
/// print(c.isIdentical(to: d))
480+
/// // Prints true
481+
/// ```
451482
///
452483
/// Comparing substrings this way includes comparing (normally) hidden
453484
/// implementation details such as the memory location of any underlying
@@ -460,6 +491,11 @@ extension Substring {
460491
}
461492
```
462493

494+
The following types will adopt the same semantic guarantees as `Substring`:
495+
* `Substring.UnicodeScalarView`
496+
* `Substring.UTF16View`
497+
* `Substring.UTF8View`
498+
463499
### `Array`
464500

465501
```swift
@@ -477,6 +513,16 @@ extension Array {
477513
/// - If `a.isIdentical(to: b)` and `b.isIdentical(to: c)` are both `true`,
478514
/// then `a.isIdentical(to: c)` is also `true`. (Transitivity)
479515
/// - If `a` and `b` are `Equatable`, then `a.isIdentical(b)` implies `a == b`
516+
/// - `a == b` does not imply `a.isIdentical(b)`
517+
///
518+
/// Values produced by copying the same value, with no intervening mutations,
519+
/// will compare identical:
520+
///
521+
/// ```swift
522+
/// let d = c
523+
/// print(c.isIdentical(to: d))
524+
/// // Prints true
525+
/// ```
480526
///
481527
/// Comparing arrays this way includes comparing (normally) hidden
482528
/// implementation details such as the memory location of any underlying
@@ -506,6 +552,16 @@ extension ArraySlice {
506552
/// - If `a.isIdentical(to: b)` and `b.isIdentical(to: c)` are both `true`,
507553
/// then `a.isIdentical(to: c)` is also `true`. (Transitivity)
508554
/// - If `a` and `b` are `Equatable`, then `a.isIdentical(b)` implies `a == b`
555+
/// - `a == b` does not imply `a.isIdentical(b)`
556+
///
557+
/// Values produced by copying the same value, with no intervening mutations,
558+
/// will compare identical:
559+
///
560+
/// ```swift
561+
/// let d = c
562+
/// print(c.isIdentical(to: d))
563+
/// // Prints true
564+
/// ```
509565
///
510566
/// Comparing arrays this way includes comparing (normally) hidden
511567
/// implementation details such as the memory location of any underlying
@@ -535,6 +591,16 @@ extension ContiguousArray {
535591
/// - If `a.isIdentical(to: b)` and `b.isIdentical(to: c)` are both `true`,
536592
/// then `a.isIdentical(to: c)` is also `true`. (Transitivity)
537593
/// - If `a` and `b` are `Equatable`, then `a.isIdentical(b)` implies `a == b`
594+
/// - `a == b` does not imply `a.isIdentical(b)`
595+
///
596+
/// Values produced by copying the same value, with no intervening mutations,
597+
/// will compare identical:
598+
///
599+
/// ```swift
600+
/// let d = c
601+
/// print(c.isIdentical(to: d))
602+
/// // Prints true
603+
/// ```
538604
///
539605
/// Comparing arrays this way includes comparing (normally) hidden
540606
/// implementation details such as the memory location of any underlying
@@ -564,6 +630,16 @@ extension Dictionary {
564630
/// - If `a.isIdentical(to: b)` and `b.isIdentical(to: c)` are both `true`,
565631
/// then `a.isIdentical(to: c)` is also `true`. (Transitivity)
566632
/// - If `a` and `b` are `Equatable`, then `a.isIdentical(b)` implies `a == b`
633+
/// - `a == b` does not imply `a.isIdentical(b)`
634+
///
635+
/// Values produced by copying the same value, with no intervening mutations,
636+
/// will compare identical:
637+
///
638+
/// ```swift
639+
/// let d = c
640+
/// print(c.isIdentical(to: d))
641+
/// // Prints true
642+
/// ```
567643
///
568644
/// Comparing dictionaries this way includes comparing (normally) hidden
569645
/// implementation details such as the memory location of any underlying
@@ -593,6 +669,16 @@ extension Set {
593669
/// - If `a.isIdentical(to: b)` and `b.isIdentical(to: c)` are both `true`,
594670
/// then `a.isIdentical(to: c)` is also `true`. (Transitivity)
595671
/// - `a.isIdentical(b)` implies `a == b`
672+
/// - `a == b` does not imply `a.isIdentical(b)`
673+
///
674+
/// Values produced by copying the same value, with no intervening mutations,
675+
/// will compare identical:
676+
///
677+
/// ```swift
678+
/// let d = c
679+
/// print(c.isIdentical(to: d))
680+
/// // Prints true
681+
/// ```
596682
///
597683
/// Comparing sets this way includes comparing (normally) hidden
598684
/// implementation details such as the memory location of any underlying set
@@ -622,12 +708,6 @@ Any Standard Library types that are copy-on-write values could be good candidate
622708
* `KeyValuePairs`
623709
* `StaticBigInt`
624710
* `StaticString`
625-
* `String.UnicodeScalarView`
626-
* `String.UTF16View`
627-
* `String.UTF8View`
628-
* `Substring.UnicodeScalarView`
629-
* `Substring.UTF16View`
630-
* `Substring.UTF8View`
631711
* `UTF8Span`
632712

633713
This proposal focuses on what we see as the most high-impact types to support from Standard Library. This proposal *is not* meant to discourage adding `isIdentical(to:)` on any of these types at some point in the future. A follow-up “second-round” proposal could focus on these remaining types.

0 commit comments

Comments
 (0)