Created
March 10, 2021 10:30
-
-
Save sindresorhus/4bbb54da90aa5df0345bc889df82979f to your computer and use it in GitHub Desktop.
Make a type `Identifiable` based on its `Hashable` hash value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Make a type `Identifiable` based on its `Hashable` hash value. | |
This can be useful when all the properties are required to represent its identifier. | |
``` | |
struct Item: Hashable, IdentifiableByHashable { | |
let title: String | |
var message: String? | |
} | |
``` | |
*/ | |
public protocol IdentifiableByHashable: Identifiable {} | |
extension IdentifiableByHashable where Self: Hashable { | |
public var id: Int { hashValue } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment