Skip to content

Commit 3af43b6

Browse files
author
Arjun Nayini
committed
[stdlib] Add other comparison operators to 0-ary tuples
Add `!=`, `<`, `<=`, `>`, `>=` to 0-ary tuples Resolves: SR-4172 (https://bugs.swift.org/browse/SR-4172)
1 parent e1a5a81 commit 3af43b6

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

stdlib/public/core/Tuple.swift.gyb

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,82 @@ comparableOperators = [
2323

2424
}%
2525

26-
/// Two tuples each of arity 0 are equal
26+
/// Returns a Boolean value indicating whether the corresponding components of
27+
/// two tuples are equal.
28+
///
29+
/// All arity zero tuples are equal.
30+
///
2731
/// - Parameters:
2832
/// - lhs: An empty tuple.
2933
/// - rhs: An empty tuple.
3034
public func ==(lhs: (), rhs: ()) -> Bool {
3135
return true
3236
}
3337

38+
/// Returns a Boolean value indicating whether any corresponding components of
39+
/// the two tuples are not equal.
40+
///
41+
/// Two tuples each of arity zero are never unequal
42+
///
43+
/// - Parameters:
44+
/// - lhs: An empty tuple.
45+
/// - rhs: An empty tuple.
46+
public func !=(lhs: (), rhs: ()) -> Bool {
47+
return false
48+
}
49+
50+
/// Returns a Boolean value indicating whether the first tuple is ordered
51+
/// before the second in a lexicographical ordering.
52+
///
53+
/// An arity zero tuple is never strictly before another arity zero tuple in a
54+
/// lexicographical ordering
55+
///
56+
/// - Parameters:
57+
/// - lhs: An empty tuple.
58+
/// - rhs: An empty tuple.
59+
public func <(lhs: (), rhs: ()) -> Bool {
60+
return false
61+
}
62+
63+
/// Returns a Boolean value indicating whether the first tuple is ordered
64+
/// before or the same as the second in a lexicographical ordering.
65+
///
66+
/// An arity zero tuple is always before or the same as another arity zero tuple
67+
/// in a lexicographical ordering
68+
///
69+
/// - Parameters:
70+
/// - lhs: An empty tuple.
71+
/// - rhs: An empty tuple.
72+
public func <=(lhs: (), rhs: ()) -> Bool {
73+
return true
74+
}
75+
76+
/// Returns a Boolean value indicating whether the first tuple is ordered
77+
/// after the second in a lexicographical ordering.
78+
///
79+
/// An arity zero tuple is never strictly after another arity zero tuple in a
80+
/// lexicographical ordering
81+
///
82+
/// - Parameters:
83+
/// - lhs: An empty tuple.
84+
/// - rhs: An empty tuple.
85+
public func >(lhs: (), rhs: ()) -> Bool {
86+
return false
87+
}
88+
89+
/// Returns a Boolean value indicating whether the first tuple is ordered
90+
/// after or the same as the second in a lexicographical ordering.
91+
///
92+
/// An arity zero tuple is always after or the same as another arity zero tuple
93+
/// in a lexicographical ordering
94+
///
95+
/// - Parameters:
96+
/// - lhs: An empty tuple.
97+
/// - rhs: An empty tuple.
98+
public func >=(lhs: (), rhs: ()) -> Bool {
99+
return true
100+
}
101+
34102
% for arity in range(2,7):
35103
% typeParams = [chr(ord("A") + i) for i in range(arity)]
36104
% tupleT = "({})".format(",".join(typeParams))

test/stdlib/Tuple.swift.gyb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ TupleTestSuite.test("Tuple/equality") {
5959
TupleTestSuite.test("Tuple/equality/sanity-check") {
6060
// sanity check all arities
6161

62-
// 0-ary tuples should be equal
6362
expectTrue(() == ())
63+
expectFalse(() != ())
64+
expectFalse(() < ())
65+
expectTrue(() <= ())
66+
expectFalse(() > ())
67+
expectTrue(() >= ())
68+
6469
% for arity in range(2, maxArity + 1):
6570
% a = str(tuple(range(1, arity + 1)))
6671
% b = "({}, 0)".format(", ".join([str(i) for i in range(1, arity)]))

0 commit comments

Comments
 (0)