Skip to content

Commit 24d4369

Browse files
authored
Merge pull request #1 from JohnSundell/id-typealias
Add shorthand ‘ID’ type alias for ‘Identifier<Self>’
2 parents 60fdbb1 + 69cb0f3 commit 24d4369

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ struct User: Identifiable {
1515
}
1616
```
1717

18-
And just like that, the above `User` identifier is now type-safe!
18+
And just like that, the above `User` identifier is now type-safe! Thanks to Swift’s type inference capabilities, it’s also possible to implement an `Identifiable` type’s `id` simply by using `ID` as its type:
19+
20+
```swift
21+
struct User: Identifiable {
22+
let id: ID
23+
let name: String
24+
}
25+
```
26+
27+
The `ID` type alias is automatically added for all `Identifiable` types, which also makes it possible to refer to `Identifier<User>` as `User.ID`.
1928

2029
## Customizing the raw type
2130

@@ -25,7 +34,7 @@ And just like that, the above `User` identifier is now type-safe!
2534
struct Article: Identifiable {
2635
typealias RawIdentifier = UUID
2736

28-
let id: Identifier<Article>
37+
let id: ID
2938
let title: String
3039
}
3140
```
@@ -58,7 +67,7 @@ articleManager.article(withID: user.id)
5867

5968
The compiler will give us an error above, since we're trying to pass an `Identifier<User>` value to a method that accepts an `Identifier<Article>` - giving us much stronger type safety than when using plain values, like `String` or `Int`, as identifiers.
6069

61-
Identity also makes it impossible to accidentially declare `id` properties of the wrong type. So the following won't compile either:
70+
Identity also makes it impossible to accidentally declare `id` properties of the wrong type. So the following won't compile either:
6271

6372
```swift
6473
struct User: Identifiable {

Sources/Identity/Identity.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import Foundation
1414
public protocol Identifiable {
1515
/// The backing raw type of this type's identifier.
1616
associatedtype RawIdentifier = String
17+
/// Shorthand type alias for this type's identifier.
18+
typealias ID = Identifier<Self>
1719
/// The ID of this instance.
18-
var id: Identifier<Self> { get }
20+
var id: ID { get }
1921
}
2022

2123
/// A type-safe identifier for a given `Value`, backed by a raw value.

Tests/IdentityTests/IdentityTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Identity
1010
final class IdentityTests: XCTestCase {
1111
func testStringBasedIdentifier() {
1212
struct Model: Identifiable {
13-
let id: Identifier<Model>
13+
let id: ID
1414
}
1515

1616
let model = Model(id: "Hello, world!")
@@ -20,7 +20,7 @@ final class IdentityTests: XCTestCase {
2020
func testIntBasedIdentifier() {
2121
struct Model: Identifiable {
2222
typealias RawIdentifier = Int
23-
let id: Identifier<Model>
23+
let id: ID
2424
}
2525

2626
let model = Model(id: 7)
@@ -30,7 +30,7 @@ final class IdentityTests: XCTestCase {
3030
func testCodableIdentifier() throws {
3131
struct Model: Identifiable, Codable {
3232
typealias RawIdentifier = UUID
33-
let id: Identifier<Model>
33+
let id: ID
3434
}
3535

3636
let model = Model(id: Identifier(rawValue: UUID()))
@@ -41,7 +41,7 @@ final class IdentityTests: XCTestCase {
4141

4242
func testIdentifierEncodedAsSingleValue() throws {
4343
struct Model: Identifiable, Codable {
44-
let id: Identifier<Model>
44+
let id: ID
4545
}
4646

4747
let model = Model(id: "I'm an ID")

0 commit comments

Comments
 (0)