Skip to content

Commit e3239e1

Browse files
authored
Add Identifier (#159)
1 parent 481d79d commit e3239e1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Identifier.swift
3+
// DataStructures
4+
//
5+
// Created by James Bean on 9/1/18.
6+
//
7+
8+
/// A type-safe identifier.
9+
///
10+
/// An `Identifier` is merely a wrapper around an `Int`, but with the added type-safety of the
11+
/// generic type parameter `Subject`.
12+
///
13+
/// let truck: Identifier<Truck> = 0
14+
/// let friend: Identifier<Friend> = 0
15+
///
16+
/// The `Subject` is not used within the type, but it exists to ensure that things like this don't
17+
/// compile:
18+
///
19+
/// truck == friend
20+
/// // Binary operator '==' cannot be applied to operands of type 'Identifier<Truck>' and
21+
/// // 'Identifier<Friend>'
22+
///
23+
public struct Identifier <Subject> {
24+
25+
let value: Int
26+
27+
// MARK: - Initializers
28+
29+
/// Creates an `Identifier` with the given `value`.
30+
public init(_ value: Int) {
31+
self.value = value
32+
}
33+
}
34+
35+
extension Identifier: ExpressibleByIntegerLiteral {
36+
37+
// MARK: - ExpressibleByIntgerLiteral
38+
39+
/// Creates an `Identifier` with the given `value`.
40+
public init(integerLiteral value: Int) {
41+
self.value = value
42+
}
43+
}
44+
45+
extension Identifier: Equatable { }
46+
extension Identifier: Hashable { }

0 commit comments

Comments
 (0)