Skip to content

Commit 62c1cf3

Browse files
authored
Merge pull request #26022 from anandabits/Identifiable
Add Identifiable protocol to the standard library
2 parents e7b1107 + 0a8f154 commit 62c1cf3

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ set(SWIFTLIB_ESSENTIAL
7575
Hashing.swift
7676
HashTable.swift
7777
ICU.swift
78+
Identifiable.swift
7879
Indices.swift
7980
InputStream.swift
8081
IntegerParsing.swift

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
"Misc": [
200200
"AtomicInt.swift",
201201
"ErrorType.swift",
202+
"Identifiable.swift",
202203
"InputStream.swift",
203204
"LifetimeManager.swift",
204205
"ManagedBuffer.swift",

stdlib/public/core/Identifiable.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// A class of types whose instances hold the value of an entity with stable identity.
14+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
15+
public protocol Identifiable {
16+
17+
/// A type representing the stable identity of the entity associated with `self`.
18+
associatedtype ID: Hashable
19+
20+
/// The stable identity of the entity associated with `self`.
21+
var id: ID { get }
22+
}
23+
24+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
25+
extension Identifiable where Self: AnyObject {
26+
public var id: ObjectIdentifier {
27+
return ObjectIdentifier(self)
28+
}
29+
}

test/stdlib/Identifiable.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
struct IdentifiableValue: Identifiable {
4+
let id = 42
5+
}
6+
7+
class IdentifiableClass: Identifiable {}

0 commit comments

Comments
 (0)