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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
discard """
output: '''
10
20
int
20
3
x as ParameterizedType[T]
x as ParameterizedType[T]
x as ParameterizedType[T]
x as ParameterizedType
x as ParameterizedType
x as CustomTypeClass
1
2
3
4
5
6
a
b
t
e
s
t
z
e
1
2
3
20
10
5
'''
"""
import typetraits, strutils
block tcomparable:
type
Comparable = concept a
(a < a) is bool
proc myMax(a, b: Comparable): Comparable =
if a < b:
return b
else:
return a
doAssert myMax(5, 10) == 10
doAssert myMax(31.3, 1.23124) == 31.3
block tconceptinclosure:
type
FonConcept = concept x
x.x is int
GenericConcept[T] = concept x
x.x is T
const L = T.name.len
Implementation = object
x: int
Closure = object
f: proc()
proc f1(x: FonConcept): Closure =
result.f = proc () =
echo x.x
proc f2(x: GenericConcept): Closure =
result.f = proc () =
echo x.x
echo GenericConcept.T.name
proc f3[T](x: GenericConcept[T]): Closure =
result.f = proc () =
echo x.x
echo x.L
let x = Implementation(x: 10)
let y = Implementation(x: 20)
let a = x.f1
let b = x.f2
let c = x.f1
let d = y.f2
let e = y.f3
a.f()
d.f()
e.f()
block overload_precedence:
type ParameterizedType[T] = object
type CustomTypeClass = concept
true
# 3 competing procs
proc a[T](x: ParameterizedType[T]) =
echo "x as ParameterizedType[T]"
proc a(x: ParameterizedType) =
echo "x as ParameterizedType"
proc a(x: CustomTypeClass) =
echo "x as CustomTypeClass"
# the same procs in different order
proc b(x: ParameterizedType) =
echo "x as ParameterizedType"
proc b(x: CustomTypeClass) =
echo "x as CustomTypeClass"
proc b[T](x: ParameterizedType[T]) =
echo "x as ParameterizedType[T]"
# and yet another order
proc c(x: CustomTypeClass) =
echo "x as CustomTypeClass"
proc c(x: ParameterizedType) =
echo "x as ParameterizedType"
proc c[T](x: ParameterizedType[T]) =
echo "x as ParameterizedType[T]"
# remove the most specific one
proc d(x: ParameterizedType) =
echo "x as ParameterizedType"
proc d(x: CustomTypeClass) =
echo "x as CustomTypeClass"
# then shuffle the order again
proc e(x: CustomTypeClass) =
echo "x as CustomTypeClass"
proc e(x: ParameterizedType) =
echo "x as ParameterizedType"
# the least specific one is a match
proc f(x: CustomTypeClass) =
echo "x as CustomTypeClass"
a(ParameterizedType[int]())
b(ParameterizedType[int]())
c(ParameterizedType[int]())
d(ParameterizedType[int]())
e(ParameterizedType[int]())
f(ParameterizedType[int]())
block templates:
template typeLen(x): int = x.type.name.len
template bunchOfChecks(x) =
x.typeLen > 3
x != 10 is bool
template stmtListExprTmpl(x: untyped): untyped =
x is int
x
type
Obj = object
x: int
Gen[T] = object
x: T
Eq = concept x, y
(x == y) is bool
NotEq = concept x, y
(x != y) is bool
ConceptUsingTemplate1 = concept x
echo x
sizeof(x) is int
bunchOfChecks x
ConceptUsingTemplate2 = concept x
stmtListExprTmpl x
template ok(x) =
static: assert(x)
template no(x) =
static: assert(not(x))
ok int is Eq
ok int is NotEq
ok string is Eq
ok string is NotEq
ok Obj is Eq
ok Obj is NotEq
ok Gen[string] is Eq
ok Gen[int] is NotEq
no int is ConceptUsingTemplate1
ok float is ConceptUsingTemplate1
no string is ConceptUsingTemplate1
ok int is ConceptUsingTemplate2
no float is ConceptUsingTemplate2
no string is ConceptUsingTemplate2
block titerable:
type
Iterable[T] = concept x
for value in x:
type(value) is T
proc sum[T](iter: Iterable[T]): T =
static: echo T.name
for element in iter:
static: echo element.type.name
result += element
doAssert sum([1, 2, 3, 4, 5]) == 15
block tmanual:
template accept(e) =
static: assert compiles(e)
template reject(e) =
static: assert(not compiles(e))
type
Container[T] = concept c
c.len is Ordinal
items(c) is T
for value in c:
type(value) is T
proc takesIntContainer(c: Container[int]) =
for e in c: echo e
takesIntContainer(@[1, 2, 3])
reject takesIntContainer(@["x", "y"])
proc takesContainer(c: Container) =
for e in c: echo e
takesContainer(@[4, 5, 6])
takesContainer(@["a", "b"])
takesContainer "test"
reject takesContainer(10)
block modifiers_in_place:
type
VarContainer[T] = concept c
put(var c, T)
AltVarContainer[T] = concept var c
put(c, T)
NonVarContainer[T] = concept c
put(c, T)
GoodContainer = object
x: int
BadContainer = object
x: int
proc put(x: BadContainer, y: int) = discard
proc put(x: var GoodContainer, y: int) = discard
template ok(x) = assert(x)
template no(x) = assert(not(x))
static:
ok GoodContainer is VarContainer[int]
ok GoodContainer is AltVarContainer[int]
no BadContainer is VarContainer[int]
no BadContainer is AltVarContainer[int]
ok GoodContainer is NonVarContainer[int]
ok BadContainer is NonVarContainer[int]
block treversable:
type
Reversable[T] = concept a
a[int] is T
a.high is int
a.len is int
a.low is int
proc get[T](s: Reversable[T], n: int): T =
s[n]
proc hi[T](s: Reversable[T]): int =
s.high
proc lo[T](s: Reversable[T]): int =
s.low
iterator reverse[T](s: Reversable[T]): T =
assert hi(s) - lo(s) == len(s) - 1
for z in hi(s).countdown(lo(s)):
yield s.get(z)
for s in @["e", "z"].reverse:
echo s
block tmonoid:
type Monoid = concept x, y
x + y is type(x)
type(z(type(x))) is type(x)
proc z(x: typedesc[int]): int = 0
doAssert(int is Monoid)
# https://github.com/nim-lang/Nim/issues/8126
type AdditiveMonoid = concept x, y, type T
x + y is T
# some redundant checks to test an alternative approaches:
type TT = type(x)
x + y is type(x)
x + y is TT
doAssert(1 is AdditiveMonoid)
block tesqofconcept:
type
MyConcept = concept x
someProc(x)
SomeSeq = seq[MyConcept]
proc someProc(x:int) = echo x
proc work (s: SomeSeq) =
for item in s:
someProc item
var s = @[1, 2, 3]
work s
block tvectorspace:
type VectorSpace[K] = concept x, y
x + y is type(x)
zero(type(x)) is type(x)
-x is type(x)
x - y is type(x)
var k: K
k * x is type(x)
proc zero(T: typedesc): T = 0
static:
assert float is VectorSpace[float]
# assert float is VectorSpace[int]
# assert int is VectorSpace
block tstack:
template reject(e) =
static: assert(not compiles(e))
type
ArrayStack = object
data: seq[int]
proc push(s: var ArrayStack, item: int) =
s.data.add item
proc pop(s: var ArrayStack): int =
return s.data.pop()
type
Stack[T] = concept var s
s.push(T)
s.pop() is T
type ValueType = T
const ValueTypeName = T.name.toUpperAscii
proc genericAlgorithm[T](s: var Stack[T], y: T) =
static:
echo "INFERRED ", T.name
echo "VALUE TYPE ", s.ValueType.name
echo "VALUE TYPE NAME ", s.ValueTypeName
s.push(y)
echo s.pop
proc implicitGeneric(s: var Stack): auto =
static:
echo "IMPLICIT INFERRED ", s.T.name, " ", Stack.T.name
echo "IMPLICIT VALUE TYPE ", s.ValueType.name, " ", Stack.ValueType.name
echo "IMPLICIT VALUE TYPE NAME ", s.ValueTypeName, " ", Stack.ValueTypeName
return s.pop()
var s = ArrayStack(data: @[])
s.push 10
s.genericAlgorithm 20
echo s.implicitGeneric
reject s.genericAlgorithm "x"
reject s.genericAlgorithm 1.0
reject "str".implicitGeneric
reject implicitGeneric(10)
import libs/[trie_database, trie]
block ttrie:
proc takeDb(d: TrieDatabase) = discard
var mdb: MemDB
takeDb(mdb)
import mvarconcept
block tvar:
# bug #2346, bug #2404
echo randomInt(5)
|