Skip to content

Commit

Permalink
feat: improve AutoEquatable/Hashable to only use stored variables, no…
Browse files Browse the repository at this point in the history
…t all variables (#157)

* change variables to storedVariables

* fix autohashable as well

* remove isStatic from AutoEquatable and AutoHashable, since storedVariables doesn't include static variables
  • Loading branch information
khanlou authored and krzysztofzablocki committed Feb 15, 2017
1 parent 644da67 commit 083058a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Templates/AutoEquatable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fileprivate func compareArrays<T>(lhs: [T], rhs: [T], compare: (_ lhs: T, _ rhs:
{% if not type.kind == "protocol" %}extension {{ type.name }}: Equatable {} {% endif %}
{% if type.supertype.based.Equatable or type.supertype.implements.AutoEquatable %} THIS WONT COMPILE, WE DONT SUPPORT INHERITANCE for AutoEquatable {% endif %}
{{ type.accessLevel }} func == (lhs: {{ type.name }}, rhs: {{ type.name }}) -> Bool {
{% for variable in type.variables %}{% if not variable.isStatic and not variable.annotations.skipEquality %}guard {% if not variable.isOptional %}{% if not variable.annotations.arrayEquality %}lhs.{{ variable.name }} == rhs.{{ variable.name }}{% else %}compareArrays(lhs: lhs.{{ variable.name }}, rhs: rhs.{{ variable.name }}, compare: ==){% endif %}{% else %}compareOptionals(lhs: lhs.{{ variable.name }}, rhs: rhs.{{ variable.name }}, compare: ==){% endif %} else { return false }{% endif %}
{% for variable in type.storedVariables %}{% if not variable.annotations.skipEquality %}guard {% if not variable.isOptional %}{% if not variable.annotations.arrayEquality %}lhs.{{ variable.name }} == rhs.{{ variable.name }}{% else %}compareArrays(lhs: lhs.{{ variable.name }}, rhs: rhs.{{ variable.name }}, compare: ==){% endif %}{% else %}compareOptionals(lhs: lhs.{{ variable.name }}, rhs: rhs.{{ variable.name }}, compare: ==){% endif %} else { return false }{% endif %}
{% endfor %}
return true
}
Expand Down
2 changes: 1 addition & 1 deletion Templates/AutoHashable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fileprivate func combineHashValues(_ initial: Int, _ other: Int) -> Int {
extension {{ type.name }}{% if not type.kind == "protocol" %}: Hashable{% endif %} {
{% if type.supertype.based.Hashable or type.supertype.implements.AutoHashable %} THIS WONT COMPILE, WE DONT SUPPORT INHERITANCE for AutoHashable {% endif %}
{{ type.accessLevel }} var hashValue: Int {
return combineHashes([{% for variable in type.variables %}{% if not variable.isStatic and not variable.annotations.skipHashing %}{% if not variable.isOptional %}{{ variable.name }}.hashValue{% else %}{{ variable.name }}?.hashValue ?? 0{% endif %}, {% endif %}{% endfor %}0])
return combineHashes([{% for variable in type.storedVariables %}{% if not variable.annotations.skipHashing %}{% if not variable.isOptional %}{{ variable.name }}.hashValue{% else %}{{ variable.name }}?.hashValue ?? 0{% endif %}, {% endif %}{% endfor %}0])
}
}
{% endif %}{% endfor %}
Expand Down

0 comments on commit 083058a

Please sign in to comment.