tmeta_typeclasses.nim 903 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. discard """
  2. output: '''12
  3. 1xxx
  4. true0
  5. 12
  6. testtest
  7. 1010
  8. 11string
  9. testtest1
  10. seq
  11. seq
  12. seq
  13. foo seq
  14. foo of numeric'''"""
  15. type
  16. TFoo[T] = object
  17. val: T
  18. T1 = auto
  19. T2 = auto
  20. Numeric = int|float
  21. proc takesExpr(x, y: auto) =
  22. echo x, y
  23. proc same(x, y: T1) =
  24. echo x, y
  25. proc takesFoo(x, y: TFoo) =
  26. echo x.val, y.val
  27. proc takes2Types[T1, T2](x,y: T1, z: T2) =
  28. echo x, y, z
  29. takesExpr(1, 2)
  30. takesExpr(1, "xxx")
  31. takesExpr[bool, int](true, 0)
  32. same(1, 2)
  33. same("test", "test")
  34. var f: TFoo[int]
  35. f.val = 10
  36. takesFoo(f, f)
  37. takes2Types(1, 1, "string")
  38. takes2Types[string, int]("test", "test", 1)
  39. proc takesSeq(x: seq) =
  40. echo "seq"
  41. takesSeq(@[1, 2, 3])
  42. takesSeq(@["x", "y", "z"])
  43. proc takesSeqOfFoos(x: seq[TFoo]) =
  44. echo "foo seq"
  45. var sf = newSeq[TFoo[int]](3)
  46. takesSeq(sf)
  47. takesSeqOfFoos(sf)
  48. proc takesFooOfNumeric(x: TFoo[Numeric]) =
  49. echo "foo of numeric"
  50. takesFooOfNumeric(sf[0])