fixes #2346
This commit is contained in:
parent
bc264618f5
commit
bf90b9c833
9 changed files with 107 additions and 97 deletions
68
tests/concepts/tusertypeclasses.nim
Normal file
68
tests/concepts/tusertypeclasses.nim
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
discard """
|
||||
output: '''Sortable
|
||||
Sortable
|
||||
Container
|
||||
true
|
||||
true
|
||||
false
|
||||
false
|
||||
false
|
||||
'''
|
||||
"""
|
||||
|
||||
import typetraits
|
||||
|
||||
type
|
||||
TObj = object
|
||||
x: int
|
||||
|
||||
Sortable = generic x, y
|
||||
(x < y) is bool
|
||||
|
||||
ObjectContainer = generic C
|
||||
C.len is Ordinal
|
||||
for v in items(C):
|
||||
v.type is tuple|object
|
||||
|
||||
proc foo(c: ObjectContainer) =
|
||||
echo "Container"
|
||||
|
||||
proc foo(x: Sortable) =
|
||||
echo "Sortable"
|
||||
|
||||
foo 10
|
||||
foo "test"
|
||||
foo(@[TObj(x: 10), TObj(x: 20)])
|
||||
|
||||
proc intval(x: int): int = 10
|
||||
|
||||
# check real and virtual fields
|
||||
type
|
||||
TFoo = generic T
|
||||
T.x
|
||||
y(T)
|
||||
intval T.y
|
||||
let z = intval(T.y)
|
||||
|
||||
proc y(x: TObj): int = 10
|
||||
|
||||
proc testFoo(x: TFoo) = discard
|
||||
testFoo(TObj(x: 10))
|
||||
|
||||
type
|
||||
Matrix[Rows, Cols: static[int]; T] = generic M
|
||||
M.M == Rows
|
||||
M.N == Cols
|
||||
M.T is T
|
||||
|
||||
MyMatrix[M, N: static[int]; T] = object
|
||||
data: array[M*N, T]
|
||||
|
||||
var x: MyMatrix[3, 3, int]
|
||||
|
||||
echo x is Matrix
|
||||
echo x is Matrix[3, 3, int]
|
||||
echo x is Matrix[3, 3, float]
|
||||
echo x is Matrix[4, 3, int]
|
||||
echo x is Matrix[3, 4, int]
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue