From 8b0848a9fd64a00253fe63afcd868a0263879406 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Wed, 15 May 2019 18:59:02 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Add=20shorthand=20=E2=80=98ID=E2=80=99=20ty?= =?UTF-8?q?pe=20alias=20for=20=E2=80=98Identifier=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Makes it more convenient to implement `Identifiable`, since the type of `Identifier` will be automatically inferred by the compiler. Plus using `Type.ID` to refer to an ID is usually nicer than `Identifier`. --- Sources/Identity/Identity.swift | 4 +++- Tests/IdentityTests/IdentityTests.swift | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/Identity/Identity.swift b/Sources/Identity/Identity.swift index 1c3f398..ab8ace8 100644 --- a/Sources/Identity/Identity.swift +++ b/Sources/Identity/Identity.swift @@ -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 /// The ID of this instance. - var id: Identifier { get } + var id: ID { get } } /// A type-safe identifier for a given `Value`, backed by a raw value. diff --git a/Tests/IdentityTests/IdentityTests.swift b/Tests/IdentityTests/IdentityTests.swift index 39b9a92..a6c0d52 100644 --- a/Tests/IdentityTests/IdentityTests.swift +++ b/Tests/IdentityTests/IdentityTests.swift @@ -10,7 +10,7 @@ import Identity final class IdentityTests: XCTestCase { func testStringBasedIdentifier() { struct Model: Identifiable { - let id: Identifier + let id: ID } let model = Model(id: "Hello, world!") @@ -20,7 +20,7 @@ final class IdentityTests: XCTestCase { func testIntBasedIdentifier() { struct Model: Identifiable { typealias RawIdentifier = Int - let id: Identifier + let id: ID } let model = Model(id: 7) @@ -30,7 +30,7 @@ final class IdentityTests: XCTestCase { func testCodableIdentifier() throws { struct Model: Identifiable, Codable { typealias RawIdentifier = UUID - let id: Identifier + let id: ID } let model = Model(id: Identifier(rawValue: UUID())) @@ -41,7 +41,7 @@ final class IdentityTests: XCTestCase { func testIdentifierEncodedAsSingleValue() throws { struct Model: Identifiable, Codable { - let id: Identifier + let id: ID } let model = Model(id: "I'm an ID") From 69cb0f32ac31d431c0c19e8630c28812f089daf4 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Wed, 15 May 2019 19:07:24 +0200 Subject: [PATCH 2/2] README: Update for new ID type alias --- README.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2ce12e3..84d9bd1 100644 --- a/README.md +++ b/README.md @@ -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` as `User.ID`. ## Customizing the raw type @@ -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
+ let id: ID let title: String } ``` @@ -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` value to a method that accepts an `Identifier
` - 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 {