texplicitgenerics.nim 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. block: # issue #16376
  2. type
  3. Matrix[T] = object
  4. data: T
  5. proc randMatrix[T](m, n: int, max: T): Matrix[T] = discard
  6. proc randMatrix[T](m, n: int, x: Slice[T]): Matrix[T] = discard
  7. template randMatrix[T](m, n: int): Matrix[T] = randMatrix[T](m, n, T(1.0))
  8. let B = randMatrix[float32](20, 10)
  9. block: # different generic param counts
  10. type
  11. Matrix[T] = object
  12. data: T
  13. proc randMatrix[T](m: T, n: T): Matrix[T] = Matrix[T](data: T(1.0))
  14. proc randMatrix[T; U: not T](m: T, n: U): (Matrix[T], U) = (Matrix[T](data: T(1.0)), default(U))
  15. let b = randMatrix[float32](20, 10)
  16. doAssert b == Matrix[float32](data: 1.0)
  17. block: # above for templates
  18. type
  19. Matrix[T] = object
  20. data: T
  21. template randMatrix[T](m: T, n: T): Matrix[T] = Matrix[T](data: T(1.0))
  22. template randMatrix[T; U: not T](m: T, n: U): (Matrix[T], U) = (Matrix[T](data: T(1.0)), default(U))
  23. let b = randMatrix[float32](20, 10)
  24. doAssert b == Matrix[float32](data: 1.0)
  25. block: # sigmatch can't handle this without pre-instantiating the type:
  26. # minimized from numericalnim
  27. type Foo[T] = proc (x: T)
  28. proc foo[T](x: T) = discard
  29. proc bar[T](f: Foo[T]) = discard
  30. bar[int](foo)
  31. block: # ditto but may be wrong minimization
  32. # minimized from measuremancer
  33. type Foo[T] = object
  34. proc foo[T](): Foo[T] = Foo[T]()
  35. # this is the actual issue but there are other instantiation problems
  36. proc bar[T](x = foo[T]()) = discard
  37. bar[int](Foo[int]())
  38. bar[int]()
  39. # alternative version, also causes instantiation issue
  40. proc baz[T](x: typeof(foo[T]())) = discard
  41. baz[int](Foo[int]())
  42. block: # issue #21346
  43. type K[T] = object
  44. template s[T](x: int) = doAssert T is K[K[int]]
  45. proc b1(n: bool | bool) = s[K[K[int]]](3)
  46. proc b2(n: bool) = s[K[K[int]]](3)
  47. template b3(n: bool) = s[K[K[int]]](3)
  48. b1(false) # Error: cannot instantiate K; got: <T> but expected: <T>
  49. b2(false) # Builds, on its own
  50. b3(false)
  51. block: # explicit generic with unresolved generic param, https://forum.nim-lang.org/t/12579
  52. var s: seq[string] = @[]
  53. proc MyMedian[T](A: var openArray[T],myCmp : proc(x,y:T):int {.nimcall.} = cmp[T]) : T =
  54. if myCmp(A[0], A[1]) == 0: s.add("1")
  55. var x = [1, 1]
  56. discard MyMedian(x) # emits "1\n"
  57. doAssert s == @["1"]
  58. block: # issue #16153
  59. type
  60. X[T] = openArray[T] | varargs[T] | seq[T]
  61. Y[T] = ref object
  62. dll: T
  63. proc initY[T](): Y[T] =
  64. new(result)
  65. proc initY[T](v: X[T]): Y[T] =
  66. new(result)
  67. for e in v:
  68. echo e
  69. var deque: Y[int] = initY[int]()