Skip to content

Map.same ​

ant
func Map.same[K, V](m: Map[K, V], n: Map[K, V]): bool

Map.same reports whether m and n are the same map, not merely equal ones.

Details ​

  • == compares keys and values, so use Map.same to tell a shared map from a copy.

Basic examples ​

A map is the same as itself, and as any other name for it:

ant
let m = Map{ a: 1 }
let n = m
Map.same(m, m) // true
Map.same(m, n) // true

An equal map built separately is not the same map:

ant
let m = Map{ a: 1 }
m == Map{ a: 1 }         // true
Map.same(m, Map{ a: 1 }) // false

Call it through . on any map:

ant
let m = Map{ a: 1 }
let n = m
m.same(n) // true

Scope ​

Two empty maps are equal, and still two maps:

ant
let a: Map[string, int] = Map{}
let b: Map[string, int] = Map{}
a == b         // true
Map.same(a, b) // false

Map.filter, Map.map, Map.merge, and Map.fromEntries build a new map, even when nothing changes:

ant
let m = Map{ a: 1, b: 2 }
Map.same(m, m.filter({ true }))           // false
Map.same(m, m.merge(Map{}))               // false
Map.same(m, Map.fromEntries(m.entries())) // false

A map held in a record field is shared by a spread of the record, since the spread copies the field, which is a reference. A field default builds a new map at every literal:

ant
type Config = { host: string, headers: Map[string, string] = Map{} }
let c = Config{ host: "a.example" }
let d = Config{ ...c, host: "b.example" }
let e = Config{ host: "a.example" }
Map.same(c.headers, d.headers) // true
Map.same(c.headers, e.headers) // false

A map passed to a function is the same map inside it, so the function can change it:

ant
func count(m: Map[string, int], word: string): Map[string, int] {
  m[word] = (m[word] ?? 0) + 1
  m
}
let words: Map[string, int] = Map{}
Map.same(count(words, "ant"), words) // true
words                                // Map{ "ant": 1 }

The maps may have any key and value types, such as records as keys, or arrays as values:

ant
type Point = { x: int, y: int }
let names = Map{ (Point{ x: 0, y: 0 }): "origin" }
let labels = names
Map.same(names, labels) // true
let groups = Map{ odd: [1, 3], even: [2] }
Map.same(groups, groups) // true

In a pipeline, the left side is m:

ant
let m = Map{ a: 1 }
let n = m
m | Map.same(n) // true

Name the arguments, in any order:

ant
let m = Map{ a: 1 }
Map.same(m: m, n: m)           // true
Map.same(n: m, m: Map{ a: 1 }) // false

Applications ​

Guard a function that moves every entry of one map into another against being given one map twice:

ant
func moveAll(from: Map[string, int], to: Map[string, int]) {
  if Map.same(from, to) {
    return
  }
  for e in from {
    to[e.key] = e.value
  }
  for k in from.keys() {
    from.remove(k)
  }
}

let pending = Map{ a: 1, b: 2 }
let stored = Map{ z: 0 }
moveAll(pending, stored)
println("${pending} ${stored}")
moveAll(stored, stored)
println(stored)
text
Map{} Map{ "z": 0, "a": 1, "b": 2 }
Map{ "z": 0, "a": 1, "b": 2 }

Properties and relations ​

Map.same is reflexive and symmetric:

ant
let m = Map{ a: 1 }
let n = m
assert(Map.same(m, m))
assert(Map.same(m, n) == Map.same(n, m))

The same map is == to itself unless it holds a NaN, since floats compare with IEEE 754 rules:

ant
let m = Map{ x: Math.NaN }
m == m         // false
Map.same(m, m) // true

When two names refer to the same map, a change through one shows through the other. For a copy it does not:

ant
let m = Map{ a: 1 }
let n = m
let copy = Map.fromEntries(m.entries())
m["b"] = 2
assert(Map.same(m, n) && n == Map{ a: 1, b: 2 })
assert(!Map.same(m, copy) && copy == Map{ a: 1 })

Reflect.same reports the same for two reflected maps:

ant
let m = Map{ a: 1 }
let t = type(Map[string, int])
assert(Reflect.same(Reflect.valueOf(t, m), Reflect.valueOf(t, m)))
assert(!Reflect.same(Reflect.valueOf(t, m), Reflect.valueOf(t, Map{ a: 1 })))

Possible issues ​

== compares contents, so it cannot tell whether two names share a map. Use Map.same when that is the question:

ant
let m = Map{ a: 1 }
let copy = m.merge(Map{})
m == copy         // true
Map.same(m, copy) // false

Both arguments have the one type Map[K, V]. A Map[string, int] and a Map[string, int | null] are different types, so comparing them is a compile error:

ant
let m = Map{ a: 1 }
let n: Map[string, int | null] = Map{ a: 1 }
println(Map.same(m, n))
// error: cannot use Map[string, int | null] where Map[string, int] is expected

A nullable map, such as a value read from another map, must be narrowed first:

ant
let m = Map{ a: 1 }
let nested = Map{ inner: m }
println(Map.same(nested["inner"], m))
// error: cannot use Map[string, int] | null where Map[K, V] is expected
ant
let m = Map{ a: 1 }
let nested = Map{ inner: m }
let found = nested["inner"]
found != null && Map.same(found, m) // true

See also ​

Array.same · Record.same · Set.same · Reflect.same · Map.merge · Map.fromEntries · == operator