mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
generic types can be used like type classes. distinct can be applied to type classes.
This commit is contained in:
34
tests/compile/ttypeclasses.nim
Normal file
34
tests/compile/ttypeclasses.nim
Normal file
@@ -0,0 +1,34 @@
|
||||
type
|
||||
TFoo[T] = object
|
||||
val: T
|
||||
|
||||
T1 = distinct expr
|
||||
T2 = distinct expr
|
||||
|
||||
proc takesExpr(x, y) =
|
||||
echo x, y
|
||||
|
||||
proc same(x, y: T1) =
|
||||
echo x, y
|
||||
|
||||
proc takesFoo(x, y: TFoo) =
|
||||
echo x.val, y.val
|
||||
|
||||
proc takes2Types(x,y: T1, z: T2) =
|
||||
echo x, y, z
|
||||
|
||||
takesExpr(1, 2)
|
||||
takesExpr(1, "xxx")
|
||||
takesExpr[bool, int](true, 0)
|
||||
|
||||
same(1, 2)
|
||||
same("test", "test")
|
||||
|
||||
var f: TFoo[int]
|
||||
f.val = 10
|
||||
|
||||
takesFoo(f, f)
|
||||
|
||||
takes2Types(1, 1, "string")
|
||||
takes2Types[string, int]("test", "test", 1)
|
||||
|
||||
Reference in New Issue
Block a user