Skip to content

Files

Latest commit

 

History

History
77 lines (52 loc) · 1.06 KB

legacy_hashing.md

File metadata and controls

77 lines (52 loc) · 1.06 KB

Pattern: Use of legacy hashing

Issue: -

Description

Prefer using the hash(into:) function instead of overriding hashValue.

Examples of correct code:

struct Foo: Hashable {
  let bar: Int = 10

  func hash(into hasher: inout Hasher) {
    hasher.combine(bar)
  }
}


class Foo: Hashable {
  let bar: Int = 10

  func hash(into hasher: inout Hasher) {
    hasher.combine(bar)
  }
}


var hashValue: Int { return 1 }
class Foo: Hashable { 
 }


class Foo: Hashable {
  let bar: String = "Foo"

  public var hashValue: String {
    return bar
  }
}


class Foo: Hashable {
  let bar: String = "Foo"

  public var hashValue: String {
    get { return bar }
    set { bar = newValue }
  }
}

Examples of incorrect code:

struct Foo: Hashable {
    let bar: Int = 10

    publicvar hashValue: Int {
        return bar
    }
}


class Foo: Hashable {
    let bar: Int = 10

    publicvar hashValue: Int {
        return bar
    }
}

Further Reading