self
The specific semantic of this method is left to the subclasses; Especially, if (and how) attributes are cloned (depth vs. shallow).
As a rule of thumb, the principle of least astonishment should be used to guide the semantic.
Note that as the returned clone depends on the semantic,
the ==
method, if redefined, should ensure the equality
between an object and its clone.
# Duplicate `self`
#
# The specific semantic of this method is left to the subclasses;
# Especially, if (and how) attributes are cloned (depth vs. shallow).
#
# As a rule of thumb, the principle of least astonishment should
# be used to guide the semantic.
#
# Note that as the returned clone depends on the semantic,
# the `==` method, if redefined, should ensure the equality
# between an object and its clone.
fun clone: SELF is abstract
lib/core/kernel.nit:417,2--428,28
# Shallow clone of the tree.
#
# ~~~
# var t = new OrderedTree[Int]
# t.add_all(null, [1, 2])
# t.add_all(1, [11, 12])
#
# assert t.clone == t
# ~~~
redef fun clone
do
var res = new OrderedTree[E]
res.add_all(null, roots)
for p, es in sub do
res.add_all(p, es)
end
return res
end
lib/ordered_tree/ordered_tree.nit:272,2--289,4
# Shallow clone of `self`
#
# ~~~
# var a = new ArrayMap[String,Int]
# a["one"] = 1
# a["two"] = 2
# var b = a.clone
# assert a == b
# a["zero"] = 0
# assert a != b
# ~~~
#
# Note that the clone is shallow and keys and values are shared between `self` and the result.
#
# ~~~
# var aa = new ArrayMap[String, Array[Int]]
# aa["two"] = [1,2]
# var bb = aa.clone
# assert aa == bb
# aa["two"].add 5
# assert aa == bb
# ~~~
redef fun clone
do
var res = new ArrayMap[K,E]
res.add_all self
return res
end
lib/core/collection/array.nit:772,2--799,4
# Shallow copy
#
# var s = new DisjointSet[Int]
# s.add_all([1,2,3,4,5])
# s.union_all([1,4,5])
# var s2 = s.clone
# assert s2.number_of_subsets == 3
# assert s2.all_in_same_subset([1,4,5]) == true
# assert s2.in_same_subset(1,2) == false
redef fun clone do return new DisjointSet[E].from(self)
lib/core/collection/union_find.nit:66,2--75,56
# Shallow clone of `self`
#
# ~~~
# var a = new ArraySet[Int]
# a.add 1
# a.add 2
# var b = a.clone
# assert a == b
# a.add 3
# assert a != b
# b.add 3
# assert a == b
# ~~~
#
# Note that the clone is shallow and keys and values are shared between `self` and the result.
#
# ~~~
# var aa = new ArraySet[Array[Int]]
# aa.add([1,2])
# var bb = aa.clone
# assert aa == bb
# aa.first.add 5
# assert aa == bb
# ~~~
redef fun clone
do
var res = new ArraySet[E]
res.add_all self
return res
end
lib/core/collection/array.nit:643,2--672,4
# Shallow clone of `self`
#
# ~~~
# var a = [1,2,3]
# var b = a.clone
# assert a == b
# a.add 4
# assert a != b
# b.add 4
# assert a == b
# ~~~
#
# Note that the clone is shallow and elements are shared between `self` and the result.
#
# ~~~
# var aa = [a]
# var bb = aa.clone
# assert aa == bb
# aa.first.add 5
# assert aa == bb
# ~~~
redef fun clone do return to_a
lib/core/collection/array.nit:488,2--509,31