templatesinconcepts.nim 908 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import typetraits
  2. template typeLen(x): int = x.type.name.len
  3. template bunchOfChecks(x) =
  4. x.typeLen > 3
  5. x != 10 is bool
  6. template stmtListExprTmpl(x: untyped): untyped =
  7. x is int
  8. x
  9. type
  10. Obj = object
  11. x: int
  12. Gen[T] = object
  13. x: T
  14. Eq = concept x, y
  15. (x == y) is bool
  16. NotEq = concept x, y
  17. (x != y) is bool
  18. ConceptUsingTemplate1 = concept x
  19. echo x
  20. sizeof(x) is int
  21. bunchOfChecks x
  22. ConceptUsingTemplate2 = concept x
  23. stmtListExprTmpl x
  24. template ok(x) =
  25. static: assert(x)
  26. template no(x) =
  27. static: assert(not(x))
  28. ok int is Eq
  29. ok int is NotEq
  30. ok string is Eq
  31. ok string is NotEq
  32. ok Obj is Eq
  33. ok Obj is NotEq
  34. ok Gen[string] is Eq
  35. ok Gen[int] is NotEq
  36. no int is ConceptUsingTemplate1
  37. ok float is ConceptUsingTemplate1
  38. no string is ConceptUsingTemplate1
  39. ok int is ConceptUsingTemplate2
  40. no float is ConceptUsingTemplate2
  41. no string is ConceptUsingTemplate2