tusertypeclasses.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. discard """
  2. matrix: "--mm:refc"
  3. output: '''Sortable
  4. Sortable
  5. Container
  6. TObj
  7. int
  8. 111 111
  9. (id: @[1, 2, 3], name: @["Vas", "Pas", "NafNaf"], age: @[10, 16, 18])
  10. '''
  11. """
  12. # todo wait for https://github.com/nim-lang/Nim/pull/20380
  13. import typetraits
  14. template reject(expr) = assert(not compiles(x))
  15. type
  16. TObj = object
  17. x: int
  18. JSonValue = object
  19. val: string
  20. Sortable = concept x, y
  21. (x < y) is bool
  22. ObjectContainer = concept C
  23. C.len is Ordinal
  24. for v in items(C):
  25. v.type is tuple|object
  26. proc foo(c: ObjectContainer) =
  27. echo "Container"
  28. proc foo(x: Sortable) =
  29. echo "Sortable"
  30. foo 10
  31. foo "test"
  32. foo(@[TObj(x: 10), TObj(x: 20)])
  33. proc intval(x: int): int = 10
  34. type
  35. TFoo = concept o, type T, ref r, var v, ptr p, static s
  36. o.x
  37. y(o) is int
  38. var str: string
  39. var intref: ref int
  40. refproc(ref T, ref int)
  41. varproc(var T)
  42. ptrproc(ptr T, str)
  43. staticproc(static[T])
  44. typeproc T
  45. T.typeproc
  46. typeproc o.type
  47. o.type.typeproc
  48. o.to(type string)
  49. o.to(type JsonValue)
  50. refproc(r, intref)
  51. varproc(v)
  52. p.ptrproc(string)
  53. staticproc s
  54. typeproc(T)
  55. const TypeName = T.name
  56. type MappedType = type(o.y)
  57. intval y(o)
  58. let z = intval(o.y)
  59. static:
  60. assert T.name.len == 4
  61. reject o.name
  62. reject o.typeproc
  63. reject staticproc(o)
  64. reject o.varproc
  65. reject T.staticproc
  66. reject p.staticproc
  67. proc y(x: TObj): int = 10
  68. proc varproc(x: var TObj) = discard
  69. proc refproc(x: ref TObj, y: ref int) = discard
  70. proc ptrproc(x: ptr TObj, y: string) = discard
  71. proc staticproc(x: static[TObj]) = discard
  72. proc typeproc(t: type TObj) = discard
  73. proc to(x: TObj, t: type string) = discard
  74. proc to(x: TObj, t: type JSonValue) = discard
  75. proc testFoo(x: TFoo) =
  76. echo x.TypeName
  77. echo x.MappedType.name
  78. testFoo(TObj(x: 10))
  79. # bug #7092
  80. type stringTest = concept x
  81. x is string
  82. let usedToFail: stringTest = "111"
  83. let working: string = "111"
  84. echo usedToFail, " ", working
  85. # bug #5868
  86. type TaggedType[T; Key: static[string]] = T
  87. proc setKey*[DT](dt: DT, key: static[string]): TaggedType[DT, key] =
  88. result = cast[type(result)](dt)
  89. type Students = object
  90. id : seq[int]
  91. name : seq[string]
  92. age: seq[int]
  93. let
  94. stud = Students(id : @[1,2,3], name : @["Vas", "Pas", "NafNaf"], age : @[10,16,18])
  95. stud2 = stud.setkey("id")
  96. echo stud2