tusertypeclasses2.nim 757 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. block:
  2. type
  3. hasFieldX = concept z
  4. z.x is int
  5. obj_x = object
  6. x: int
  7. ref_obj_x = ref object
  8. x: int
  9. ref_to_obj_x = ref obj_x
  10. p_o_x = ptr obj_x
  11. v_o_x = var obj_x
  12. template check(x) =
  13. static: assert(x)
  14. check obj_x is hasFieldX
  15. check ref_obj_x is hasFieldX
  16. check ref_to_obj_x is hasFieldX
  17. check p_o_x is hasFieldX
  18. check v_o_x is hasFieldX
  19. block:
  20. type
  21. Foo = concept x
  22. x.isFoo
  23. Bar = distinct float
  24. template isFoo(x: Bar): untyped = true
  25. proc foo(x: var Foo) =
  26. float(x) = 1.0
  27. proc foo2(x: var Bar) =
  28. float(x) = 1.0
  29. proc foo3(x: var (Bar|SomeNumber)) =
  30. float(x) = 1.0
  31. proc foo4(x: var any) =
  32. float(x) = 1.0
  33. var x: Bar
  34. foo(x)
  35. foo2(x)
  36. foo3(x)
  37. foo4(x)