Skip to content

[5.1] Add Identifiable protocol to the standard library #26378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ set(SWIFTLIB_ESSENTIAL
Hashing.swift
HashTable.swift
ICU.swift
Identifiable.swift
Indices.swift
InputStream.swift
IntegerParsing.swift
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"Misc": [
"AtomicInt.swift",
"ErrorType.swift",
"Identifiable.swift",
"InputStream.swift",
"LifetimeManager.swift",
"ManagedBuffer.swift",
Expand Down
29 changes: 29 additions & 0 deletions stdlib/public/core/Identifiable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// A class of types whose instances hold the value of an entity with stable identity.
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public protocol Identifiable {

/// A type representing the stable identity of the entity associated with `self`.
associatedtype ID: Hashable

/// The stable identity of the entity associated with `self`.
var id: ID { get }
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension Identifiable where Self: AnyObject {
public var id: ObjectIdentifier {
return ObjectIdentifier(self)
}
}
7 changes: 7 additions & 0 deletions test/stdlib/Identifiable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift

struct IdentifiableValue: Identifiable {
let id = 42
}

class IdentifiableClass: Identifiable {}