Skip to content

Commit

Permalink
Merge pull request #1 from JohnSundell/id-typealias
Browse files Browse the repository at this point in the history
Add shorthand ‘ID’ type alias for ‘Identifier<Self>’
  • Loading branch information
JohnSundell authored May 15, 2019
2 parents 60fdbb1 + 69cb0f3 commit 24d4369
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ struct User: Identifiable {
}
```

And just like that, the above `User` identifier is now type-safe!
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:

```swift
struct User: Identifiable {
let id: ID
let name: String
}
```

The `ID` type alias is automatically added for all `Identifiable` types, which also makes it possible to refer to `Identifier<User>` as `User.ID`.

## Customizing the raw type

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

let id: Identifier<Article>
let id: ID
let title: String
}
```
Expand Down Expand Up @@ -58,7 +67,7 @@ articleManager.article(withID: user.id)

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.

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

```swift
struct User: Identifiable {
Expand Down
4 changes: 3 additions & 1 deletion Sources/Identity/Identity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import Foundation
public protocol Identifiable {
/// The backing raw type of this type's identifier.
associatedtype RawIdentifier = String
/// Shorthand type alias for this type's identifier.
typealias ID = Identifier<Self>
/// The ID of this instance.
var id: Identifier<Self> { get }
var id: ID { get }
}

/// A type-safe identifier for a given `Value`, backed by a raw value.
Expand Down
8 changes: 4 additions & 4 deletions Tests/IdentityTests/IdentityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Identity
final class IdentityTests: XCTestCase {
func testStringBasedIdentifier() {
struct Model: Identifiable {
let id: Identifier<Model>
let id: ID
}

let model = Model(id: "Hello, world!")
Expand All @@ -20,7 +20,7 @@ final class IdentityTests: XCTestCase {
func testIntBasedIdentifier() {
struct Model: Identifiable {
typealias RawIdentifier = Int
let id: Identifier<Model>
let id: ID
}

let model = Model(id: 7)
Expand All @@ -30,7 +30,7 @@ final class IdentityTests: XCTestCase {
func testCodableIdentifier() throws {
struct Model: Identifiable, Codable {
typealias RawIdentifier = UUID
let id: Identifier<Model>
let id: ID
}

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

func testIdentifierEncodedAsSingleValue() throws {
struct Model: Identifiable, Codable {
let id: Identifier<Model>
let id: ID
}

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

0 comments on commit 24d4369

Please sign in to comment.