@Hashable is a Swift macro for adding Hashable conformance. It is particularly useful when synthesised conformance is not possible, such as with classes or a struct with 1 or more non-hashable properties.
The @Hashable macro is applied to the type that will conform to Hashable and the Hashed macro is applied to each of the properties that should contribute to the Hashable conformance.
import HashableMacro
/// A struct that uses the ``stringProperty`` and ``intProperty`` for `Hashable` conformance.
@HashablestructMyStruct{
// Any property that is hashable is supported.
@HashedletstringProperty:String
// Works on private properties, too.
@HashedprivateletintProperty:Int
// Non-decorated properties are ignored
letnotHashableType:NotHashableType}All decorated properties are included in both the == and hash(into:) implementations, ensuring the contract of Hashable is upheld:
Two instances that are equal must feed the same values to
Hasherinhash(into:), in the same order.
DocC documentation for HashableMacro is hosted by Swift Package Index. Most of the implementation can be seen in the Macros.swift file.
The @NotHashed macro can be applied to properties that should not be included in the Hashable conformance. If this macro is used to decorate a property the @Hashed macro should not be used to decorate a property in the same type.
This can be useful for types that have a smaller number of non-hashable properties than hashable properties.
/// A struct that uses the ``stringProperty`` and ``intProperty`` for `Hashable` conformance.
@HashablestructMyStruct{
// Implicitly used for `Hashable` conformance
letstringProperty:String
// Implicitly used for `Hashable` conformance
privateletintProperty:Int
// Explicitly excluded from `Hashable` conformance
@NotHashedletnotHashableType:NotHashableType}If the @Hashable macro is added but no properties are decorated with @Hashed or @NotHashed then all stored properties will be used.
/// A struct that uses the ``stringProperty`` and ``intProperty`` for `Hashable` conformance.
@HashablestructMyStruct{
// Implicitly used for `Hashable` conformance
letstringProperty:String
// Implicitly used for `Hashable` conformance
privateletintProperty:Int
// Implicitly excluded from `Hashable` conformance
varcomputedProperty:Bool{
intProperty >0}}One (fairly minor) advantage of this over adding Hashable conformance without the macro is that you can see the code being produce via Right Click → Expand Macro.
When a type implements NSObjectProtocol (e.g. it inherits from NSObject) it should override hash and isEqual(_:), not hash(into:) and ==. @Hashable detects when it is attached to a type conforming to NSObjectProtocol and will provide the hash property and isEqual(_:) function instead.
@Hashable will also provide an isEqual(to:) function that takes a parameter that matches Self, which will also have an appropriately named Objective-C function.
import HashableMacro
@HashablefinalclassPerson:NSObject{@Hashedvarname:String=""}extensionPerson{overridevarhash:Int{varhasher=Hasher()
hasher.combine(self.name)return hasher.finalize()}}extensionPerson{overridefunc isEqual(_ object:Any?)->Bool{guardlet object = object as?Personelse{returnfalse}guardtype(of:self)==type(of: object)else{returnfalse}returnself.isEqual(to: object)}@objc(isEqualToPerson:)func isEqual(to object:Person)->Bool{returnself.name == object.name
}}When the @Hashable macro is added to a class the generated hash(into:) function is marked final. This is because subclasses should not overload ==. There are many reasons why this can be a bad idea, but specifically in Swift this does not work because:
!=is not part of theEquatableprotocol, but rather an extension onEquatable, causing it to always use the==implementation from the class that addsEquatableconformance- It is possible to overload
!=but this is still not a good idea because...
- It is possible to overload
- Anything that uses generics to compare the values, for example
XCTAssertEqual, will use the==implementation from the class that addsEquatableconformance- It is possible to work around this by using a separate function, in a similar way to
NSObject, which is then called from==
- It is possible to work around this by using a separate function, in a similar way to
If this is an issue for your usage you can pass finalHashInto: false to the macro, but it will not attempt to call super or use properties from the superclass.
This is not something the macro aims to solve.