lib: add `Object::is_same_instance` that will replace `is`
authorJean Privat <jean@pryen.org>
Tue, 26 Nov 2013 20:02:35 +0000 (15:02 -0500)
committerJean Privat <jean@pryen.org>
Tue, 26 Nov 2013 20:02:35 +0000 (15:02 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/collection/hash_collection.nit
lib/standard/kernel.nit

index 9a53f99..7f73c3b 100644 (file)
@@ -44,7 +44,7 @@ private abstract class HashCollection[K: Object, N: HashNode[Object]]
        fun node_at(k: K): nullable N
        do
                # cache: `is` is used instead of `==` because it is a faster filter (even if not exact)
-               if k is _last_accessed_key then return _last_accessed_node
+               if k.is_same_instance(_last_accessed_key) then return _last_accessed_node
 
                var res = node_at_idx(index_at(k), k)
                _last_accessed_key = k
@@ -58,7 +58,7 @@ private abstract class HashCollection[K: Object, N: HashNode[Object]]
                var c = _array[i]
                while c != null do
                        var ck = c._key
-                       if ck is k or ck == k then # prefilter with `is` because the compiler is not smart enought yet
+                       if ck.is_same_instance(k) or ck == k then # FIXME prefilter because the compiler is not smart enought yet
                                break
                        end
                        c = c._next_in_bucklet
index bf158a3..bbc2b70 100644 (file)
@@ -35,11 +35,15 @@ interface Object
        # Unless specific code, you should not use this method.
        fun is_same_type(other: Object): Bool is intern
 
+       # Return true if `self` and `other` are the same instance.
+       # Unless specific code, you should use `==` instead.
+       fun is_same_instance(other: nullable Object): Bool do return self is other #is intern
+
        # Have `self` and `other` the same value?
        ##
        # The exact meaning of "same value" is let to the subclasses.
-       # Implicitly, the default implementation, is `is`
-       fun ==(other: nullable Object): Bool do return self is other
+       # Implicitly, the default implementation, is `is_same_instance`
+       fun ==(other: nullable Object): Bool do return self.is_same_instance(other)
 
        # Have `self` and `other` different values?
        ##