0

In Java it is possible to automatically generate implementations of toString(), hashCode(), and equals(Object), for value objects using the AutoValue library. The advantages are (among others) that these methods are automatically updated when adding new fields. My question is whether there is equivalent functionality available for Swift (either built-in or as a library)?

Example

A simple example, demonstrating the current situation in Swift. Consider the following struct:

struct PlainStruct {
  let a:String
  let b:Int
  let c:Double
}

In order to have it conform to Hashable I modified it as follows:

struct HashableStruct : Hashable {
  let a:String
  let b:Int
  let c:Double
  
  var hashValue: Int {
    return 5381 &+ a.hashValue &+ b.hashValue &+ c.hashValue
  }
  
  static func ==(lhs: HashableStruct, rhs: HashableStruct) -> Bool {
    return lhs.a == rhs.a
        && lhs.b == rhs.b
        && lhs.c == rhs.c
  }
}
     

The code added, although straightforward, introduces an additional burden for programmers, especially when the software further evolves (as it usually does). Ideally, I would like to be able to simply add an attribute (like an annotation in Java) to the struct:

@autovalue
struct HashableStruct : Hashable {
  let a:String
  let b:Int
  let c:Double
}

Currently this is not possible as there is no way to specify custom attributes in Swift. Is there another way to implement this in Swift?

Motivation for AutoValue in Java

The following text is from the AutoValue page, it motivates why AutoValue was created. I expect that the same reasoning is applicable in the context of Swift, but I'm not as experienced in Swift as in Java so correct me if I'm wrong.

Value classes are extremely common in Java projects. These are classes for which you want to treat any two instances with suitably equal field values as interchangeable. That's right: we're talking about those classes where you wind up implementing equals, hashCode and toString in a bloated, repetitive, formulaic yet error-prone fashion.

Writing these methods the first time is not too bad, with the aid of a few helper methods and IDE templates. But once written they continue to burden reviewers, editors and future readers. Their wide expanses of boilerplate sharply decrease the signal-to-noise ratio of your code... and they love to harbor hard-to-spot bugs.

AutoValue provides an easier way to create immutable value classes, with a lot less code and less room for error, while not restricting your freedom to code almost any aspect of your class exactly the way you want it.

Community
  • 1
  • 1
rinde
  • 1,181
  • 1
  • 8
  • 20
  • 1
    In Swift 4.1, the compiler can automatically synthesise a conformance to `Hashable` for structures with `Hashable` properties (and enums with `Hashable` associated values) – see https://github.com/apple/swift-evolution/blob/master/proposals/0185-synthesize-equatable-hashable.md – Hamish Mar 21 '18 at 12:48
  • Related: https://stackoverflow.com/questions/46782139/how-may-i-test-the-equivalency-of-enumeration-cases-with-associated-values-in-sw. – Martin R Mar 21 '18 at 12:48

0 Answers0