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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
|
discard """
output: '''
4
3
(weight: 17.0, color: 100)
perm: 22 det: 22
TMatrix[3, 3, system.int]
3
@[0.9, 0.1]
U[3]
U[(f: 3)]
U[[3]]
b()
@[1, 2]
@[3, 4]
1
concrete 88
123
1
2
3
!!Hi!!
G:0,1:0.1
G:0,1:0.1
H:1:0.1
0
(foo: none(seq[Foo]), s: "")
(foo: some(@[(a: "world", bar: none(Bar))]), s: "hello,")
@[(a: "hey", bar: none(Bar))]
'''
joinable: false
"""
import macros, sequtils, sets, sugar, tables, typetraits
block t88:
type
BaseClass[V] = object of RootObj
b: V
proc new[V](t: typedesc[BaseClass], v: V): BaseClass[V] =
BaseClass[V](b: v)
proc baseMethod[V](v: BaseClass[V]): V = v.b
proc overriddenMethod[V](v: BaseClass[V]): V = v.baseMethod
type
ChildClass[V] = object of BaseClass[V]
c: V
proc new[V](t: typedesc[ChildClass], v1, v2: V): ChildClass[V] =
ChildClass[V](b: v1, c: v2)
proc overriddenMethod[V](v: ChildClass[V]): V = v.c
let c = ChildClass[string].new("Base", "Child")
doAssert c.baseMethod == "Base"
doAssert c.overriddenMethod == "Child"
block t4528:
type GenericBase[T] = ref object of RootObj
type GenericSubclass[T] = ref object of GenericBase[T]
proc foo[T](g: GenericBase[T]) = discard
var bar: GenericSubclass[int]
foo(bar)
block t1050_5597:
type ArrayType[T] = distinct T
proc arrayItem(a: ArrayType): auto =
static: echo(name(type(a).T))
result = (type(a).T)(4)
var arr: ArrayType[int]
echo arrayItem(arr)
# bug #5597
template fail() = "what"
proc g[T](x: var T) =
x.fail = 3
type
Obj = object
fail: int
var y: Obj
g y
block t1789:
type
Foo[N: static[int]] = object
proc bindStaticN[N](foo: Foo[N]) =
var ar0: array[3, int]
var ar1: array[N, int]
var ar2: array[1..N, int]
var ar3: array[0..(N+10), float]
echo N
var f: Foo[3]
f.bindStaticN
# case 2
type
ObjectWithStatic[X, Y: static[int], T] = object
bar: array[X * Y, T] # this one works
AliasWithStatic[X, Y: static[int], T] = array[X * Y, T]
var
x: ObjectWithStatic[1, 2, int]
y: AliasWithStatic[2, 3, int]
# case 3
type
Bar[N: static[int], T] = object
bar: array[N, T]
proc `[]`[N, T](f: Bar[N, T], n: range[0..(N - 1)]): T =
doAssert high(n) == N-1
result = f.bar[n]
var b: Bar[3, int]
doAssert b[2] == 0
block t3977:
type Foo[N: static[int]] = object
proc foo[N](x: Foo[N]) =
let n = N
doAssert N == n
var f1: Foo[42]
f1.foo
block t5570:
type
BaseFruit[T] = object of RootObj
color: T
Banana[T] = object of BaseFruit[uint32]
weight: T
macro printTypeName(typ: typed): untyped =
echo "type ", getType(typ).repr
proc setColor[K](self: var BaseFruit[K], c: int) =
printTypeName(self.color)
self.color = uint32(c)
var x: Banana[float64]
x.weight = 17
printTypeName(x.color)
x.setColor(100)
echo x
block t5643:
type
Matrix[M, N: static[int], T: SomeFloat] = object
data: ref array[N * M, T]
Matrix64[M, N: static[int]] = Matrix[M, N, float64]
proc zeros64(M,N: static[int]): Matrix64[M,N] =
new result.data
for i in 0 ..< (M * N):
result.data[i] = 0'f64
proc bar[M,N: static[int], T](a: Matrix[M,N,T], b: Matrix[M,N,T]) =
discard
let a = zeros64(2,2)
bar(a,a)
# https://github.com/nim-lang/Nim/issues/5643
#
# The test case was failing here, because the compiler failed to
# detect the two matrix instantiations as the same type.
#
# The root cause was that the `T` type variable is a different
# type after the first Matrix type has been matched.
#
# Sigmatch was failing to match the second version of `T`, but
# due to some complex interplay between tyOr, tyTypeDesc and
# tyGenericParam this was allowed to went through. The generic
# instantiation of the second matrix was incomplete and the
# generic cache lookup failed, producing two separate types.
block t5683:
type Matrix[M,N: static[int]] = array[M, array[N, float]]
proc det[M,N](a: Matrix[M,N]): int = N*10 + M
proc perm[M,N](a: Matrix[M,N]): int = M*10 + N
const
a = [ [1.0, 2.0]
, [3.0, 4.0]
]
echo "perm: ", a.perm, " det: ", a.det
# This tests multiple instantiations of a generic
# proc involving static params:
type
Vector64[N: static[int]] = ref array[N, float64]
Array64[N: static[int]] = array[N, float64]
proc vector[N: static[int]](xs: Array64[N]): Vector64[N] =
new result
for i in 0 ..< N:
result[i] = xs[i]
let v1 = vector([1.0, 2.0, 3.0, 4.0, 5.0])
let v2 = vector([1.0, 2.0, 3.0, 4.0, 5.0])
let v3 = vector([1.0, 2.0, 3.0, 4.0])
block t7794:
type
Data[T:SomeNumber, U:SomeFloat] = ref object
x: T
value*: U
var d = Data[int, float64](x:10.int, value:2'f64)
doAssert d.x == 10
doAssert d.value == 2.0
block t8403:
proc sum[T](s: seq[T], R: typedesc): R =
var sum: R = 0
for x in s:
sum += R(x)
return sum
doAssert @[1, 2, 3].sum(float) == 6.0
block t8439:
type
Cardinal = enum
north, east, south, west
proc foo[cardinal: static[Cardinal]](): int = 1
doAssert foo[north]() == 1
block t8694:
when true:
# Error: undeclared identifier: '|'
proc bar[T](t:T): bool =
runnableExamples:
type Foo = int | float
true
doAssert bar(0)
when true:
# ok
proc bar(t:int): bool =
runnableExamples:
type Foo = int | float
true
doAssert bar(0)
when true:
# Error: undeclared identifier: '|'
proc bar(t:typedesc): bool =
runnableExamples:
type Foo = int | float
true
doAssert bar(int)
block t9130:
when true:
# stack overflow
template baz1(iter: untyped): untyped =
runnableExamples:
import sugar
proc fun(a: proc(x:int): int) = discard
baz1(fun(x:int => x))
discard
proc foo1[A](ts: A) =
baz1(ts)
when true:
# ok
template baz2(iter: untyped): untyped =
runnableExamples:
import sugar
proc fun(a: proc(x:int): int) = discard
baz2(fun(x:int => x))
discard
proc foo2(ts: int) =
baz2(ts)
when true:
# stack overflow
template baz3(iter: untyped): untyped =
runnableExamples:
baz3(fun(x:int => x))
discard
proc foo3[A](ts: A) =
baz3(ts)
block t1056:
type
TMatrix[N,M: static[int], T] = object
data: array[0..N*M-1, T]
TMat2[T] = TMatrix[2,2,T]
proc echoMatrix(a: TMatrix) =
echo a.type.name
echo TMatrix.N
proc echoMat2(a: TMat2) =
echo TMat2.M
var m = TMatrix[3,3,int](data: [1,2,3,4,5,6,7,8,9])
echoMatrix m
block t4884:
type
Vec[N: static[int], T] = object
arr*: array[N, T]
Mat[N,M: static[int], T] = object
arr: array[N, Vec[M,T]]
var m : Mat[3,3,float]
var strMat : Mat[m.N, m.M, string]
var lenMat : Mat[m.N, m.M, int]
block t2221:
var tblo: TableRef[string, int]
doAssert tblo == nil
block t2304:
type TV2[T:SomeNumber] = array[0..1, T]
proc newV2T[T](x, y: T=0): TV2[T] = [x, y]
let x = newV2T[float](0.9, 0.1)
echo(@x)
block t2752:
proc myFilter[T](it: (iterator(): T), f: (proc(anything: T):bool)): (iterator(): T) =
iterator aNameWhichWillConflict(): T {.closure.}=
for x in it():
if f(x):
yield x
result = aNameWhichWillConflict
iterator testIt():int {.closure.}=
yield -1
yield 2
#let unusedVariable = myFilter(testIt, (x: int) => x > 0)
proc onlyPos(it: (iterator(): int)): (iterator(): int)=
iterator aNameWhichWillConflict(): int {.closure.}=
var filtered = onlyPos(myFilter(it, (x:int) => x > 0))
for x in filtered():
yield x
result = aNameWhichWillConflict
let x = onlyPos(testIt)
block t5106:
block:
type T = distinct int
proc `+`(a, b: T): T =
T(int(a) + int(b))
type U[F: static[T]] = distinct int
proc `+`[P1, P2: static[T]](a: U[P1], b: U[P2]): U[P1 + P2] =
U[P1 + P2](int(a) + int(b))
var a = U[T(1)](1)
var b = U[T(2)](2)
var c = a + b
echo c.type.name
block:
type T = object
f: int
proc `+`(a, b: T): T =
T(f: a.f + b.f)
type U[F: static[T]] = distinct int
proc `+`[P1, P2: static[T]](a: U[P1], b: U[P2]): U[P1 + P2] =
U[P1 + P2](int(a) + int(b))
var a = U[T(f: 1)](1)
var b = U[T(f: 2)](2)
var c = a + b
echo c.type.name
block:
type T = distinct array[0..0, int]
proc `+`(a, b: T): T =
T([array[0..0, int](a)[0] + array[0..0, int](b)[0]])
type U[F: static[T]] = distinct int
proc `+`[P1, P2: static[T]](a: U[P1], b: U[P2]): U[P1 + P2] =
U[P1 + P2](int(a) + int(b))
var a = U[T([1])](1)
var b = U[T([2])](2)
var c = a + b
echo c.type.name
block t3055:
proc b(t: int | string)
proc a(t: int) = b(t)
proc b(t: int | string) = echo "b()"
a(1)
# test recursive generics still work:
proc fac[T](x: T): T =
if x == 0: return 1
else: return fac(x-1)*x
doAssert fac(6) == 720
doAssert fac(5.0) == 120.0
# test recursive generic with forwarding:
proc fac2[T](x: T): T
doAssert fac2(6) == 720
doAssert fac2(5.0) == 120.0
proc fac2[T](x: T): T =
if x == 0: return 1
else: return fac2(x-1)*x
block t1187:
type
TEventArgs = object
skip: bool
TEventHandler[T] = proc (e: var TEventArgs, data: T) {.closure.}
TEvent[T] = object
#handlers: seq[TEventHandler[T]] # Does not work
handlers: seq[proc (e: var TEventArgs, d: T) {.closure.}] # works
TData = object
x: int
TSomething = object
s: TEvent[TData]
proc init[T](e: var TEvent[T]) =
e.handlers.newSeq(0)
#proc add*[T](e: var TEvent[T], h: proc (e: var TEventArgs, data: T) {.closure.}) =
# this line works
proc add[T](e: var TEvent[T], h: TEventHandler[T]) =
# this line does not work
e.handlers.add(h)
proc main () =
var something: TSomething
something.s.init()
var fromOutside = 4711
something.s.add() do (e: var TEventArgs, data: TData):
var x = data.x
x = fromOutside
main()
block t1919:
type
Base[M] = object of RootObj
a : M
Sub1[M] = object of Base[M]
b : int
Sub2[M] = object of Sub1[M]
c : int
var x: Sub2[float]
doAssert x.a == 0.0
block t5756:
type
Vec[N : static[int]] = object
x: int
arr: array[N, int32]
Mat[M,N: static[int]] = object
x: int
arr: array[M, Vec[N]]
proc vec2(x,y:int32) : Vec[2] =
result.arr = [x,y]
result.x = 10
proc mat2(a,b: Vec[2]): Mat[2,2] =
result.arr = [a,b]
result.x = 20
const M = mat2(vec2(1, 2), vec2(3, 4))
let m1 = M
echo @(m1.arr[0].arr)
echo @(m1.arr[1].arr)
proc foo =
let m2 = M
echo m1.arr[0].arr[0]
foo()
block t7854:
type
Stream = ref StreamObj
StreamObj = object of RootObj
InhStream = ref InhStreamObj
InhStreamObj = object of Stream
f: string
proc newInhStream(f: string): InhStream =
new(result)
result.f = f
var val: int
let str = newInhStream("input_file.json")
block:
# works:
proc load[T](data: var T, s: Stream) =
discard
load(val, str)
block:
# works
proc load[T](s: Stream, data: T) =
discard
load(str, val)
block:
# broken
proc load[T](s: Stream, data: var T) =
discard
load(str, val)
block t5864:
proc defaultStatic(s: openArray, N: static[int] = 1): int = N
proc defaultGeneric[T](a: T = 2): int = a
let a = [1, 2, 3, 4].defaultStatic()
let b = defaultGeneric()
doAssert a == 1
doAssert b == 2
block t3498:
template defaultOf[T](t: T): untyped = (var d: T; d)
doAssert defaultOf(1) == 0
# assignment using template
template tassign[T](x: var seq[T]) =
x = @[1, 2, 3]
var y: seq[int]
tassign(y) #<- x is expected = @[1, 2, 3]
tassign(y)
doAssert y[0] == 1
doAssert y[1] == 2
doAssert y[2] == 3
block t3499:
proc foo[T](x: proc(): T) =
echo "generic ", x()
proc foo(x: proc(): int) =
echo "concrete ", x()
# note the following 'proc' is not .closure!
foo(proc (): auto {.nimcall.} = 88)
# bug #3499 last snippet fixed
# bug 705 last snippet fixed
block t797:
proc foo[T](s:T):string = $s
type IntStringProc = proc(x: int): string
var f1 = IntStringProc(foo)
var f2: proc(x: int): string = foo
var f3: IntStringProc = foo
echo f1(1), f2(2), f3(3)
for x in map([1,2,3], foo): echo x
block t4658:
var x = 123
proc twice[T](f: T -> T): T -> T = (x: T) => f(f(x))
proc quote(s: string): string = "!" & s & "!"
echo twice(quote)("Hi")
block t4589:
type SimpleTable[TKey, TVal] = TableRef[TKey, TVal]
template newSimpleTable(TKey, TVal: typedesc): SimpleTable[TKey, TVal] = newTable[TKey, TVal]()
var fontCache : SimpleTable[string, SimpleTable[int32, int]]
fontCache = newSimpleTable(string, SimpleTable[int32, int])
block t4600:
template foo(x: untyped): untyped = echo 1
template foo(x,y: untyped): untyped = echo 2
proc bar1[T](x: T) = foo(x)
proc bar2(x: float) = foo(x,x)
proc bar3[T](x: T) = foo(x,x)
block t4672:
type
EnumContainer[T: enum] = object
v: T
SomeEnum {.pure.} = enum
A,B,C
proc value[T: enum](this: EnumContainer[T]): T =
this.v
var enumContainer: EnumContainer[SomeEnum]
discard enumContainer.value()
block t4863:
type
G[i,j: static[int]] = object
v:float
H[j: static[int]] = G[0,j]
proc p[i,j: static[int]](x:G[i,j]) = echo "G:", i, ",", j, ":", x.v
proc q[j: static[int]](x:H[j]) = echo "H:", j, ":", x.v
var
g0 = G[0,1](v: 0.1)
h0:H[1] = g0
p(g0)
p(h0)
q(h0)
block t1684:
type
BaseType {.inheritable pure.} = object
idx: int
DerivedType {.final pure.} = object of BaseType
proc index[Toohoo: BaseType](h: Toohoo): int {.inline.} = h.idx
proc newDerived(idx: int): DerivedType {.inline.} = DerivedType(idx: idx)
let d = newDerived(2)
doAssert(d.index == 2)
block t5632:
type Option[T] = object
proc point[A](v: A, t: typedesc[Option[A]]): Option[A] =
discard
discard point(1, Option)
block t7247:
type n8 = range[0'i8..127'i8]
var tab = initHashSet[n8]()
doAssert tab.contains(8) == false
block t3717:
type
Foo[T] = object
a: T
Foo1[T] = Foo[T] | int
proc foo[T](s: Foo1[Foo[T]]): T =
5
var f: Foo[Foo[int]]
discard foo(f)
block: # issue #9458
type
Option[T] = object
val: T
has: bool
Bar = object
proc none(T: typedesc): Option[T] =
discard
proc foo[T](self: T; x: Option[Bar] = Bar.none) =
discard
foo(1)
# bug #8426
type
MyBool[T: uint] = range[T(0)..T(1)] # Works
var x: MyBool[uint]
echo x
# x = 2 # correctly prevented
type
MyBool2 = range[uint(0)..uint(1)] # Error ordinal or float type expected
# bug #10396
import options, strutils
type
Foo {.acyclic.} = object
a: string
bar: Option[Bar]
Bar {.acyclic.} = object
foo: Option[seq[Foo]] # if this was just Option[Foo], everything works fine
s: string
proc getBar(x: string): Bar
proc intoFoos(ss: seq[string]): seq[Foo] =
result = @[]
for s in ss:
let spl = s.split(',')
if spl.len > 1:
result.add Foo(a: spl[0],
bar: some(getBar(spl[1])))
else:
result.add Foo(a: s,
bar: none[Bar]())
proc getBar(x: string): Bar =
let spl = x.split(' ')
result =
if spl.len > 1:
Bar(foo: some(spl[1..high(spl)].intoFoos),
s: spl[0])
else:
Bar(foo: none[seq[Foo]](),
s: "")
proc fakeReadLine(): string = "hey"
echo getBar(fakeReadLine()) # causes error
echo getBar("hello, world") # causes error
discard $getBar(fakeReadLine()) # causes error
discard $getBar("hello, world") # causes error
discard getBar(fakeReadLine()) # no error
discard getBar("hello, world") # no error
echo intoFoos(fakeReadLine().split(' ')) # no error, works as expected
# bug #14990
type
Tile3 = Tile2
Tile2 = Tile
Tile[n] = object
a: n
var a: Tile3[int]
block: # Ensure no segfault from constraint
type
Regex[A: SomeOrdinal] = ref object
val: Regex[A]
MyConstraint = (seq or enum or set)
MyOtherType[A: MyConstraint] = ref object
val: MyOtherType[A]
var
a = Regex[int]()
b = Regex[bool]()
c = MyOtherType[seq[int]]()
block: # https://github.com/nim-lang/Nim/issues/20416
type
Item[T] = object
link:ptr Item[T]
data:T
KVSeq[A,B] = seq[(A,B)]
MyTable[A,B] = object
data: KVSeq[A,B]
Container[T] = object
a: MyTable[int,ref Item[T]]
proc p1(sg:Container) = discard # Make sure that a non parameterized 'Container' argument still compiles
proc p2[T](sg:Container[T]) = discard
var v : Container[int]
p2(v)
|