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.

Property definitions

core $ Cloneable :: 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

matrix $ Matrix :: clone
	# Create a new clone of this matrix
	redef fun clone
	do
		var c = new Matrix(width, height)
		for i in [0..width*height[ do c.items[i] = items[i]
		return c
	end
lib/matrix/matrix.nit:134,2--140,4

ordered_tree $ OrderedTree :: clone
	# 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

poset $ POSet :: clone
	redef fun clone do return sub(self)
lib/poset/poset.nit:388,2--36

core $ Set :: clone
	redef fun clone do return union(self)
lib/core/collection/abstract_collection.nit:523,2--38

core $ ArrayMap :: clone
	# 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

core $ DisjointSet :: clone
	# 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

core $ ArraySet :: clone
	# 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

core $ Buffer :: clone
	# ~~~
	# var b = new Buffer
	# b.append("Buffer!")
	# var c = b.clone
	# assert b == c
	# ~~~
	redef fun clone do
		var cln = new Buffer.with_cap(byte_length)
		cln.append self
		return cln
	end
lib/core/text/abstract_text.nit:1550,2--1560,4

core $ Array :: clone
	# 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

core $ String :: clone
	redef fun clone do return self
lib/core/text/abstract_text.nit:1495,2--31