core :: Object :: is_same_instance
self
and other
are the same instance (i.e. same identity).var a = new Buffer
var b = a
var c = new Buffer
assert a.is_same_instance(b)
assert not a.is_same_instance(c)
assert a == c
Obviously, the identity of an object is preserved even if the object is mutated.
var x = [1]
var y = x
x.add 2
assert x.is_same_instance(y)
Unless specific code, you should use ==
instead of is_same_instance
because
most of the time is it the semantic (and user-defined) comparison that make sense.
Moreover, relying on is_same_instance
on objects you do not control
might have unexpected effects when libraries reuse objects or intern them.
# Return true if `self` and `other` are the same instance (i.e. same identity).
#
# ~~~
# var a = new Buffer
# var b = a
# var c = new Buffer
# assert a.is_same_instance(b)
# assert not a.is_same_instance(c)
# assert a == c # because both buffers are empty
# ~~~
#
# Obviously, the identity of an object is preserved even if the object is mutated.
#
# ~~~
# var x = [1]
# var y = x
# x.add 2
# assert x.is_same_instance(y)
# ~~~
#
# Unless specific code, you should use `==` instead of `is_same_instance` because
# most of the time is it the semantic (and user-defined) comparison that make sense.
#
# Moreover, relying on `is_same_instance` on objects you do not control
# might have unexpected effects when libraries reuse objects or intern them.
fun is_same_instance(other: nullable Object): Bool is intern
lib/core/kernel.nit:109,2--134,61