Record.same
func Record.same[T: {}](a: T, b: T): boolRecord.same reports whether a and b are the same record, not merely equal ones. The bound {} admits any record type.
Details
- Returns
falsefor two different records, even when they are==. - A nullable record, such as a value read from a map, is a compile error; narrow it first.
Basic examples
A record is the same as itself, and as any other name for it:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
let q = p
Record.same(p, p) // true
Record.same(p, q) // trueA copy made with a spread is equal, and not the same record:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
p == Point{ ...p } // true
Record.same(p, Point{ ...p }) // falseScope
Two records of an empty type are equal, and still two records:
type Empty = {}
let a = Empty{}
let b = Empty{}
a == b // true
Record.same(a, b) // falseA spread copies the fields, and a field that holds a record holds a reference, so the copy shares it:
type Point = { x: float64, y: float64 }
type Line = { from: Point, to: Point }
let l = Line{ from: Point{ x: 0.0, y: 0.0 }, to: Point{ x: 1.0, y: 1.0 } }
let m = Line{ ...l, to: Point{ x: 2.0, y: 2.0 } }
Record.same(l, m) // false
Record.same(l.from, m.from) // true
Record.same(l.to, m.to) // falseA record read from an array, or passed to a function, is the same record:
type User = { name: string }
func first(us: Array[User]): User {
us[0]
}
let ann = User{ name: "Ann" }
let users = [ann]
Record.same(users[0], ann) // true
Record.same(first(users), ann) // trueAny record type works, including prelude records such as Error, and a union of records such as Result:
let e = Error{ message: "x" }
Record.same(e, e) // true
Record.same(e, Error.wrap(e, "load")) // false
let r: Result[int, Error] = Result.ok(1)
Record.same(r, r) // trueA caught panic raised again and caught again is a new record, equal to the first:
let p = Panic.catch({ panic("boom") })
if p is Err {
let again = Panic.catch({ Panic.raise(p.error) })
if again is Err {
println(again.error == p.error)
println(Record.same(again.error, p.error))
}
}true
falseIn a pipeline, the left side is a:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
let q = p
p | Record.same(q) // trueName the arguments, in any order:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
Record.same(a: p, b: p) // true
Record.same(b: p, a: Point{ ...p }) // falseApplications
Skip a save when an update returns its input unchanged. rename returns the same record when the name does not change, and a new one when it does:
type User = { id: int, name: string }
func rename(u: User, name: string): User {
if u.name == name { u } else { User{ ...u, name } }
}
let ann = User{ id: 1, name: "Ann" }
for name in ["Ann", "Anna"] {
let next = rename(ann, name)
if Record.same(next, ann) {
println("${name}: unchanged")
} else {
println("${name}: save ${next}")
}
}Ann: unchanged
Anna: save User{ id: 1, name: "Anna" }Find a loop in a linked list. Two nodes may be equal and still be different nodes, so the walk keeps the nodes it has seen and compares by identity:
type Node = { value: int, next: Node | null }
func hasLoop(head: Node): bool {
let seen: Array[Node] = []
var node: Node | null = head
while node != null {
let n = node
if seen.some({ Record.same(x, n) }) {
return true
}
seen.push(n)
node = n.next
}
false
}
let c = Node{ value: 1, next: null }
let b = Node{ value: 1, next: c }
let a = Node{ value: 1, next: b }
println(hasLoop(a))
c.next = a
println(hasLoop(a))false
trueProperties and relations
Record.same is reflexive and symmetric:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
let q = p
assert(Record.same(p, p))
assert(Record.same(p, q) == Record.same(q, p))The same record is == to itself unless it holds a NaN, in a field or anywhere below one, since floats compare with IEEE 754 rules:
type Point = { x: float64, y: float64 }
let p = Point{ x: Math.NaN, y: 0.0 }
p == p // false
Record.same(p, p) // trueWhen two names refer to the same record, a change through one shows through the other. For a copy it does not:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
let q = p
let r = Point{ ...p }
q.x = 5.0
assert(Record.same(p, q) && p.x == 5.0)
assert(!Record.same(p, r) && r.x == 1.0)Reflect.same reports the same for two reflected records:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
let t = type(Point)
assert(Reflect.same(Reflect.valueOf(t, p), Reflect.valueOf(t, p)))
assert(!Reflect.same(Reflect.valueOf(t, p), Reflect.valueOf(t, Point{ ...p })))Possible issues
Record.same is not reached through . on a record, since a record's functions are the ones declared on its type:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
println(p.same(p)) // error: Point has no field or function sameBoth arguments have the one type T, fixed by the first argument. Two record types with the same fields are still different types:
type Point = { x: float64, y: float64 }
type Vec = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
println(Record.same(p, Vec{ ...p })) // error: cannot use Vec where Point is expectedA nullable record, such as a value read from a map, is not a record. Narrow it first:
type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
let m = Map{ "a": p }
println(Record.same(m["a"], p))
// error: T would be Point | null, which is not assignable to its bound {}type Point = { x: float64, y: float64 }
let p = Point{ x: 1.0, y: 2.0 }
let m = Map{ "a": p }
let found = m["a"]
found != null && Record.same(found, p) // trueArrays, maps, and sets have their own identity functions:
println(Record.same([1], [1]))
// error: T would be Array[int], which is not assignable to its bound {}See also
Array.same · Map.same · Set.same · Reflect.same · Error.wrap · == operator
