Skip to content

Commit

Permalink
Do not use HashCode for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 4, 2023
1 parent 9263555 commit 3af6d1f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
28 changes: 22 additions & 6 deletions src/Visp.Runtime.Library/Value.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,33 @@ type Value =
| HashSet of HashSet
| Pair of Pair

member this.TypeName =
match this with
| Nil -> "Value.Nil"
| Unit -> "Value.Unit"
| String _ -> "Value.String"
| Symbol _ -> "Value.Symbol"
| Keyword _ -> "Value.Keyword"
| Number _ -> "Value.Number"
| Char _ -> "Value.Char"
| Bool _ -> "Value.Bool"
| Any _ -> "Value.Any"
| Atom _ -> "Value.Atom"
| List _ -> "Value.List"
| Vector _ -> "Value.Vector"
| HashMap _ -> "Value.HashMap"
| HashSet _ -> "Value.HashSet"
| Pair _ -> "Value.Pair"

override this.GetHashCode() =
System.HashCode.Combine(
this.GetType(),
match this with
| Vector v -> v.GetHashCode()
| List v -> v.GetHashCode()
| String v -> HashCode.Combine("string", v.GetHashCode())
| Symbol v -> HashCode.Combine("symbol", v.GetHashCode())
| Keyword v -> HashCode.Combine("keyword", v.GetHashCode())
| String v -> HashCode.Combine("string", v)
| Symbol v -> HashCode.Combine("symbol", v)
| Keyword v -> HashCode.Combine("keyword", v)
| Number v -> v.GetHashCode()
| Char v -> v.GetHashCode()
| Bool v -> v.GetHashCode()
Expand Down Expand Up @@ -105,9 +123,7 @@ type Value =
| (HashMap lhs, HashMap rhs) -> lhs.CompareTo(rhs)
| (HashSet lhs, HashSet rhs) -> lhs.CompareTo(rhs)
| (Pair lhs, Pair rhs) -> lhs.CompareTo(rhs)
| (lhs, rhs) ->
(lhs.GetType().GetHashCode() :> IComparable<_>)
.CompareTo(rhs.GetType().GetHashCode())
| (lhs, rhs) -> lhs.TypeName.CompareTo(rhs.TypeName)

static member list(it: Value seq) = List(ValueList(List.ofSeq it))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:this-is-a-key "value" "string as key" "string value" 0 "int as key"}
{:this-is-a-key "value" 0 "int as key" "string as key" "string value"}
"string value"
"value"
()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#{:this-is-a-key "int as key" "string as key" "string value" "value" 0}
#{:this-is-a-key 0 "int as key" "string as key" "string value" "value"}
()

ExitCode: 0

0 comments on commit 3af6d1f

Please sign in to comment.