summary refs log tree commit diff stats
path: root/tests/proc/texplicitgenerics.nim
blob: 833d77b3b790e30bbfc0e7f2d49001c400caef06 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
block: # issue #16376
  type
    Matrix[T] = object
      data: T
  proc randMatrix[T](m, n: int, max: T): Matrix[T] = discard
  proc randMatrix[T](m, n: int, x: Slice[T]): Matrix[T] = discard
  template randMatrix[T](m, n: int): Matrix[T] = randMatrix[T](m, n, T(1.0))
  let B = randMatrix[float32](20, 10)

block: # different generic param counts 
  type
    Matrix[T] = object
      data: T
  proc randMatrix[T](m: T, n: T): Matrix[T] = Matrix[T](data: T(1.0))
  proc randMatrix[T; U: not T](m: T, n: U): (Matrix[T], U) = (Matrix[T](data: T(1.0)), default(U))
  let b = randMatrix[float32](20, 10)
  doAssert b == Matrix[float32](data: 1.0)

block: # above for templates 
  type
    Matrix[T] = object
      data: T
  template randMatrix[T](m: T, n: T): Matrix[T] = Matrix[T](data: T(1.0))
  template randMatrix[T; U: not T](m: T, n: U): (Matrix[T], U) = (Matrix[T](data: T(1.0)), default(U))
  let b = randMatrix[float32](20, 10)
  doAssert b == Matrix[float32](data: 1.0)

block: # sigmatch can't handle this without pre-instantiating the type:
  # minimized from numericalnim
  type Foo[T] = proc (x: T)
  proc foo[T](x: T) = discard
  proc bar[T](f: Foo[T]) = discard
  bar[int](foo)

block: # ditto but may be wrong minimization
  # minimized from measuremancer
  type Foo[T] = object
  proc foo[T](): Foo[T] = Foo[T]()
  # this is the actual issue but there are other instantiation problems
  proc bar[T](x = foo[T]()) = discard
  bar[int](Foo[int]())
  bar[int]()
  # alternative version, also causes instantiation issue
  proc baz[T](x: typeof(foo[T]())) = discard
  baz[int](Foo[int]())

block: # issue #21346
  type K[T] = object
  template s[T](x: int) = doAssert T is K[K[int]]
  proc b1(n: bool | bool) = s[K[K[int]]](3)
  proc b2(n: bool)        = s[K[K[int]]](3)
  template b3(n: bool)    = s[K[K[int]]](3)
  b1(false)     # Error: cannot instantiate K; got: <T> but expected: <T>
  b2(false)     # Builds, on its own
  b3(false)