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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
discard """
output: '''4
0
4
4
1
2
3
yes int
string int'''
joinable: false
"""
import hashes
type
Comparable = concept # no T, an atom
proc cmp(a, b: Self): int
ToStringable = concept
proc `$`(a: Self): string
Hashable = concept ## the most basic of identity assumptions
proc hash(x: Self): int
proc `==`(x, y: Self): bool
Swapable = concept
proc swap(x, y: var Self)
proc h(x: Hashable) =
echo x
h(4)
when true:
proc compare(a: Comparable) =
echo cmp(a, a)
compare(4)
proc dollar(x: ToStringable) =
echo x
when true:
dollar 4
dollar "4"
#type D = distinct int
#dollar D(4)
when true:
type
Iterable[Ix] = concept
iterator items(c: Self): Ix
proc g[Tu](it: Iterable[Tu]) =
for x in it:
echo x
g(@[1, 2, 3])
proc hs(x: Swapable) =
var y = x
swap y, y
hs(4)
type
Indexable[T] = concept # has a T, a collection
proc `[]`(a: Self; index: int): T # we need to describe how to infer 'T'
# and then we can use the 'T' and it must match:
proc `[]=`(a: var Self; index: int; value: T)
proc len(a: Self): int
proc indexOf[T](a: Indexable[T]; value: T) =
echo "yes ", T
block:
var x = @[1, 2, 3]
indexOf(x, 4)
import tables, typetraits
type
Dict[K, V] = concept
proc `[]`(s: Self; k: K): V
proc `[]=`(s: var Self; k: K; v: V)
proc d[K2, V2](x: Dict[K2, V2]) =
echo K2, " ", V2
var x = initTable[string, int]()
d(x)
type Monoid = concept
proc `+`(x, y: Self): Self
proc z(t: typedesc[Self]): Self
proc z(x: typedesc[int]): int = 0
doAssert int is Monoid
|