mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
closes #6013, closes #7009, closes #9190, closes #12487, closes #12831,
closes #13184, closes #13252, closes #14860, closes #14877, closes
#14894, closes #14917, closes #16153, closes #16439, closes #17779,
closes #18074, closes #18202, closes #18314, closes #18648, closes
#19063, closes #19446, closes #20065, closes #20367, closes #22126,
closes #22820, closes #22888, closes #23020, closes #23287, closes
#23510
(cherry picked from commit aeb3fe9505)
80 lines
2.5 KiB
Nim
80 lines
2.5 KiB
Nim
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)
|
|
|
|
block: # explicit generic with unresolved generic param, https://forum.nim-lang.org/t/12579
|
|
var s: seq[string] = @[]
|
|
proc MyMedian[T](A: var openArray[T],myCmp : proc(x,y:T):int {.nimcall.} = cmp[T]) : T =
|
|
if myCmp(A[0], A[1]) == 0: s.add("1")
|
|
var x = [1, 1]
|
|
discard MyMedian(x) # emits "1\n"
|
|
doAssert s == @["1"]
|
|
|
|
block: # issue #16153
|
|
type
|
|
X[T] = openArray[T] | varargs[T] | seq[T]
|
|
Y[T] = ref object
|
|
dll: T
|
|
|
|
proc initY[T](): Y[T] =
|
|
new(result)
|
|
|
|
proc initY[T](v: X[T]): Y[T] =
|
|
new(result)
|
|
for e in v:
|
|
echo e
|
|
|
|
var deque: Y[int] = initY[int]()
|