diff options
Diffstat (limited to 'std/prelude/clone.pk')
-rw-r--r-- | std/prelude/clone.pk | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/std/prelude/clone.pk b/std/prelude/clone.pk index 5b16b8b..5d11cdb 100644 --- a/std/prelude/clone.pk +++ b/std/prelude/clone.pk @@ -1,28 +1,36 @@ -## std.clone: Interfaces for explicitly Clonable types. +## std.clone: Classes for explicitly Clonable types. ## This module is imported by default. -## The Clone interface. Any type implementing Clone is cloneable. +## The Clone class. Any type implementing Clone is cloneable. ## Data structures built with pointers and references are pretty much ## the only things that are *not* implicitly clonable. -pub type Clone = interface - clone(lent Self): Self # todo +pub type Clone = class + clone(lent Self): Self ## Generic implementation of `clone` for structs. -pub func clone[T: struct](self: T): T = +## fixme: this relies on no component of a struct being a ref or ptr +## cloning an rc is fine?? +pub func clone[T: Clone](self: struct[T]): struct[T] = ... ## Generic implementation of `clone` for tuples. -pub func clone[T: tuple](self: T): T = +pub func clone[T: Clone](self: tuple[T]): tuple[T] = ... ## Implementation of `clone` for arrays of any size. -pub func clone[T, S: static[uint]](self: array[T, S]): array[T, S] = - ... +pub func clone[T: Clone, size: static[uint]](self: array[T, size]): array[T, size] = + var res: array[T, size] + for i in 0 .. size do + res.push(self.i.clone) ## Implementation of `clone` for lists. -pub func clone[T](self: list[T]): list[T] = - ... +pub func clone[T: Clone](self: list[T]): list[T] = + var res: list[T] + for elem in self do + res.push(elem.clone) # the explicit dependency on Clone gives us this ## Implementation of `clone` for strings. pub func clone(self: str): str = - ... + var res: str + for char in self do + res.push(char) |