Skip to content

Commit b23db00

Browse files
committed
Comparable implies Equality
((x >= y) && (x <= y)) == (x == y) ((x > y) || (x < y)) == (x != y) Swift SVN r3173
1 parent fa5c235 commit b23db00

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

docs/Generics.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ Generic functions can be overloaded based entirely on constraints. For example,
724724
consider a binary search algorithm::
725725
726726
func binarySearch<C : EnumerableCollection>(collection : C, value : C.Element) -> C.RangeType
727-
requires C.Element : Ordered {
727+
requires C.Element : Comparable {
728728
// We can perform log(N) comparisons, but EnumerableCollection only supports linear
729729
// walks, so this is linear time
730730
}
@@ -734,7 +734,7 @@ consider a binary search algorithm::
734734
}
735735

736736
func binarySearch<C : EnumerableCollection>(collection : C, value : C.Element) -> C.RangeType
737-
requires C.Element : Ordered, C.RangeType : RandomAccessRange {
737+
requires C.Element : Comparable, C.RangeType : RandomAccessRange {
738738
// We can perform log(N) comparisons and log(N) range splits, so this is logarithmic time
739739
}
740740

docs/TypeChecker.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,16 @@ parameter polymorphism that enables polymorphic types and
330330
functions. For example, one can implement a ``min`` function as,
331331
e.g.,::
332332

333-
func min<T : Ordered>(x : T, y : T) -> T {
333+
func min<T : Comparable>(x : T, y : T) -> T {
334334
if y < x { return y }
335335
return x
336336
}
337337

338338
Here, ``T`` is effectively a type variable that can be replaced with
339339
any concrete type, so long as that type conforms to the protocol
340-
``Ordered``. The type of ``min`` is (internally) written as ``<T : Ordered> (x :
340+
``Comparable``. The type of ``min`` is (internally) written as ``<T : Comparable> (x :
341341
T, y : T) -> T``, which can be read as "for all ``T``, where ``T``
342-
conforms to ``Ordered``, the type of the function is ``(x : T, y : T)
342+
conforms to ``Comparable``, the type of the function is ``(x : T, y : T)
343343
-> T``. Different uses of the ``min`` function may have different
344344
bindings for the type variable ``T``.
345345

@@ -351,7 +351,7 @@ function, and produces a monomorphic function type based on the
351351
newly-generated type variables. For example, the first occurrence of
352352
the declaration reference expression ``min`` would result in a type
353353
``(x : T0, y : T0) -> T0``, where ``T0`` is a fresh type variable, as
354-
well as the subtype constraint ``T0 < Ordered``, which expresses
354+
well as the subtype constraint ``T0 < Comparable``, which expresses
355355
protocol conformance. The next occurrence of the declaration reference
356356
expression ``min`` would produce the type ``(x : T1, y : T1) -> T1``,
357357
where ``T1`` is a fresh type variable (and therefore distinct from

0 commit comments

Comments
 (0)