Quirrel4.41.0

types.Class.clone

Method on every class
clone(): any

Always fails. clone is one of the type-methods every built-in delegate carries, but the VM's generic clone operation explicitly refuses a class.

Return value

Never returns.

Errors

Always throws cloning a class.

Notes

Cloning is defined for tables, arrays, and instances (see types.Instance.clone), each of which copies its own stored values into a fresh object. A class is a shared definition, not a bag of instance data, so there is nothing meaningful for clone to copy; the same restriction applies to userdata. To get a new, independent class, use instance() to make an instance instead, or __merge to build a new class out of this one's members.

clone is a compiler keyword by default (the clone x operator), so SomeClass.clone() does not even parse unless the file starts with the #forbid-clone-operator directive; every example on this page and on types.Instance.clone carries it for that reason.

Example

examples/types/class/clone.nut
#forbid-clone-operator
class Foo { x = 1 }
try {
  Foo.clone()
} catch (e) {
  println("Foo.clone() throws:", e)
}
Output:
Foo.clone() throws: cloning a class

See also

instanceCreates a new instance of the class without running its constructor.
callCreates a new instance of the class and runs its constructor, the same as writing ClassName(...). Exists so a class value obtained indirectly (through a variable, a table slot, or getclass()) can still be instantiated through a uniform .call(...) spelling shared with functions.
cloneCreates a new instance of the same class, copying this instance's own field values into it.
classclass index