Skip to content

Commit b2a7283

Browse files
authored
Merge pull request #149 from dn-m/newtype
Add Comparable conformance to NewType
2 parents ce548fb + c00f660 commit b2a7283

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Sources/DataStructures/NewType.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ extension NewType {
1717
}
1818
}
1919

20+
extension NewType where Value: Comparable {
21+
public static func < (lhs: Self, rhs: Self) -> Bool {
22+
return lhs.value < rhs.value
23+
}
24+
}
25+
2026
extension NewType where Value: ExpressibleByIntegerLiteral {
2127
public init(integerLiteral value: Value.IntegerLiteralType) {
2228
self.init(value: Value(integerLiteral: value))

Tests/DataStructuresTests/NewTypeTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import DataStructures
1010

1111
class NewTypeTests: XCTestCase {
1212

13+
func testComparable() {
14+
struct C: NewType, Comparable { let value: Int }
15+
let a = C(1)
16+
let c = C(2)
17+
XCTAssert(a < c)
18+
XCTAssert(c > a)
19+
}
20+
1321
func testExpressibleByIntegerLiteral() {
1422
struct I: NewType, ExpressibleByIntegerLiteral { let value: Int }
1523
let _: I = 42

0 commit comments

Comments
 (0)