Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Created March 10, 2021 10:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sindresorhus/4bbb54da90aa5df0345bc889df82979f to your computer and use it in GitHub Desktop.
Save sindresorhus/4bbb54da90aa5df0345bc889df82979f to your computer and use it in GitHub Desktop.
Make a type `Identifiable` based on its `Hashable` hash value.
/**
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