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

nitc :: astbuilder $ ANode :: clone
	redef fun clone: SELF
	do
		# By default the clone abort to avoid surprises
		print "The clone method is not implemented for the `{self.class_name}` class"
		abort
	end
src/astbuilder.nit:884,2--889,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

nitc :: astbuilder $ AQid :: clone
	redef fun clone: SELF
	do
		var clone_n_qualified = n_qualified
		if n_qualified != null then clone_n_qualified = n_qualified.clone
		return new AQid.init_aqid(clone_n_qualified, n_id.clone)
	end
src/astbuilder.nit:835,2--840,4

nitc :: astbuilder $ AQclassid :: clone
	redef fun clone: SELF
	do
		return new AQclassid.init_aqclassid(n_qualified.clone, n_id)
	end
src/astbuilder.nit:821,2--824,4

nitc :: astbuilder $ ASignature :: clone
	redef fun clone: SELF
	do
		var ntype = n_type
		if ntype != null then ntype = n_type.clone
		return new ASignature.init_asignature(null, n_params.clone, null, ntype)
	end
src/astbuilder.nit:751,2--756,4

nitc :: astbuilder $ AParam :: clone
	redef fun clone: SELF
	do
		var ntype = n_type
		if ntype != null then ntype = n_type.clone
		return new AParam.make(variable, ntype)
	end
src/astbuilder.nit:769,2--774,4

nitc :: astbuilder $ AType :: clone
	redef fun clone: SELF
	do
		return new AType.make(mtype)
	end
src/astbuilder.nit:510,2--513,4

nitc :: astbuilder $ AQualified :: clone
	redef fun clone: SELF
	do
		return new AQualified.init_aqualified(n_id.clone, n_classid)
	end
src/astbuilder.nit:828,2--831,4

nitc :: astbuilder $ TId :: clone
	redef fun clone: SELF
	do
		return new TId.init_tk(location)
	end
src/astbuilder.nit:844,2--847,4

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

nitc :: astbuilder $ ABlockExpr :: clone
	redef fun clone: SELF
	do
		var clone = new ABlockExpr.make(mtype)
		for expr in self.n_expr do
			clone.add(expr.clone)
		end
		return clone
	end
src/astbuilder.nit:810,2--817,4

nitc :: astbuilder $ ASelfExpr :: clone
	redef fun clone: SELF
	do
		return new ASelfExpr.make(self.variable, self.mtype)
	end
src/astbuilder.nit:668,2--671,4

nitc :: astbuilder $ AIntegerExpr :: clone
	redef fun clone: SELF
	do
		return new AIntegerExpr.make(value, mtype)
	end
src/astbuilder.nit:528,2--531,4

nitc :: astbuilder $ AFloatExpr :: clone
	redef fun clone: SELF
	do
		return new AFloatExpr.make(value, mtype)
	end
src/astbuilder.nit:544,2--547,4

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

nitc :: astbuilder $ AImplicitSelfExpr :: clone
	redef fun clone: SELF
	do
		var self_clone = new AImplicitSelfExpr.make(variable, mtype)
		self_clone.is_sys = is_sys
		return self_clone
	end
src/astbuilder.nit:676,2--681,4

nitc :: astbuilder $ ACharExpr :: clone
	redef fun clone: SELF
	do
		var self_clone = new ACharExpr.make(self.value, mtype, n_char.text)
		return self_clone
	end
src/astbuilder.nit:593,2--597,4

nitc :: astbuilder $ ATrueExpr :: clone
	redef fun clone: SELF
	do
		return new ATrueExpr.make(mtype)
	end
src/astbuilder.nit:559,2--562,4

nitc :: astbuilder $ AFalseExpr :: clone
	redef fun clone: SELF
	do
		return new AFalseExpr.make(mtype)
	end
src/astbuilder.nit:574,2--577,4

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

nitc :: astbuilder $ ANodes :: clone
	redef fun clone: SELF
	do
		var clone_anodes = new ANodes[E](self.parent)
		for node in self do
			clone_anodes.add(node.clone)
		end
		return clone_anodes
	end
src/astbuilder.nit:871,2--878,4

nitc $ FlowHashMap :: clone
	# Not a deep copy.
	redef fun clone do return new FlowHashMap[K, V].from(self)
src/saf/saf_base.nit:111,2--112,59

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

nitc $ FlowHashSet :: clone
	redef fun clone do return new FlowHashSet[E].from(self)
src/saf/saf_base.nit:120,2--56

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