tbindoncevsbindmany.nim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. template accept(x) =
  2. static: assert(compiles(x))
  3. template reject(x) =
  4. static: assert(not compiles(x))
  5. type
  6. ObjectWithNumber = concept obj
  7. obj.number is int
  8. Foo[T] = object
  9. x: T
  10. type A = object
  11. anumber: int
  12. type B = object
  13. bnumber: int
  14. proc number(a: A): int = a.anumber
  15. proc number(b: B): int = b.bnumber
  16. proc notDistincConcept1(a: ObjectWithNumber, b: ObjectWithNumber) = discard
  17. proc notDistincConcept2(a, b: ObjectWithNumber) = discard
  18. proc distinctConcept1(a, b: distinct ObjectWithNumber) = discard
  19. proc distinctConcept2(a: ObjectWithNumber, b: distinct ObjectWithNumber) = discard
  20. proc distinctConcept3(a: distinct ObjectWithNumber, b: ObjectWithNumber) = discard
  21. proc distinctConcept4(a: distinct ObjectWithNumber, b: distinct ObjectWithNumber) = discard
  22. var a = A(anumber: 5)
  23. var b = B(bnumber: 6)
  24. accept notDistincConcept1(a, a)
  25. accept notDistincConcept1(b, b)
  26. reject notDistincConcept2(a, b)
  27. accept notDistincConcept2(a, a)
  28. accept notDistincConcept2(b, b)
  29. reject notDistincConcept2(a, b)
  30. accept distinctConcept1(a, b)
  31. accept distinctConcept2(a, b)
  32. accept distinctConcept3(a, b)
  33. accept distinctConcept4(a, b)
  34. proc nonDistincGeneric1(a: Foo, b: Foo) = discard
  35. proc nonDistincGeneric2(a, b: Foo) = discard
  36. proc distinctGeneric1(a, b: distinct Foo) = discard
  37. proc distinctGeneric2(a: distinct Foo, b: Foo) = discard
  38. proc distinctGeneric3(a: Foo, b: distinct Foo) = discard
  39. proc distinctGeneric4(a: distinct Foo, b: distinct Foo) = discard
  40. var f1 = Foo[int](x: 10)
  41. var f2 = Foo[string](x: "x")
  42. accept nonDistincGeneric1(f1, f1)
  43. accept nonDistincGeneric1(f2, f2)
  44. reject nonDistincGeneric1(f1, f2)
  45. accept nonDistincGeneric2(f1, f1)
  46. accept nonDistincGeneric2(f2, f2)
  47. reject nonDistincGeneric2(f1, f2)
  48. accept distinctGeneric1(f1, f1)
  49. accept distinctGeneric2(f1, f1)
  50. accept distinctGeneric3(f1, f1)
  51. accept distinctGeneric4(f1, f1)