From 0a8f154ab2ba50c4649bcc03d5e7d0a038bda6e0 Mon Sep 17 00:00:00 2001 From: Matthew Johnson Date: Tue, 9 Jul 2019 10:14:34 -0500 Subject: [PATCH] Add Identifiable protocol to the standard library --- stdlib/public/core/CMakeLists.txt | 1 + stdlib/public/core/GroupInfo.json | 1 + stdlib/public/core/Identifiable.swift | 29 +++++++++++++++++++++++++++ test/stdlib/Identifiable.swift | 7 +++++++ 4 files changed, 38 insertions(+) create mode 100644 stdlib/public/core/Identifiable.swift create mode 100644 test/stdlib/Identifiable.swift diff --git a/stdlib/public/core/CMakeLists.txt b/stdlib/public/core/CMakeLists.txt index ecb5a5a0743a6..a86190606b585 100644 --- a/stdlib/public/core/CMakeLists.txt +++ b/stdlib/public/core/CMakeLists.txt @@ -75,6 +75,7 @@ set(SWIFTLIB_ESSENTIAL Hashing.swift HashTable.swift ICU.swift + Identifiable.swift Indices.swift InputStream.swift IntegerParsing.swift diff --git a/stdlib/public/core/GroupInfo.json b/stdlib/public/core/GroupInfo.json index a09e00d5c39c3..15a1c7f6e59e6 100644 --- a/stdlib/public/core/GroupInfo.json +++ b/stdlib/public/core/GroupInfo.json @@ -199,6 +199,7 @@ "Misc": [ "AtomicInt.swift", "ErrorType.swift", + "Identifiable.swift", "InputStream.swift", "LifetimeManager.swift", "ManagedBuffer.swift", diff --git a/stdlib/public/core/Identifiable.swift b/stdlib/public/core/Identifiable.swift new file mode 100644 index 0000000000000..c9a9147abf8c8 --- /dev/null +++ b/stdlib/public/core/Identifiable.swift @@ -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) + } +} diff --git a/test/stdlib/Identifiable.swift b/test/stdlib/Identifiable.swift new file mode 100644 index 0000000000000..a801356424574 --- /dev/null +++ b/test/stdlib/Identifiable.swift @@ -0,0 +1,7 @@ +// RUN: %target-typecheck-verify-swift + +struct IdentifiableValue: Identifiable { + let id = 42 +} + +class IdentifiableClass: Identifiable {} \ No newline at end of file