aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/clone.pk
blob: 0ddfcf657561fa3732c54c7ac2d2724439ab6c73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## std.clone: Classes for explicitly Clonable types.
## This module is imported by default.

## 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 = class
  clone(lent Self): Self

## Generic implementation of `clone` for structs.
## 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: Clone](self: tuple[T]): tuple[T] =
  ...

## Implementation of `clone` for arrays of any size.
pub func clone[T: Clone, size: const 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: 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)