tdistinct.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. discard """
  2. output: '''
  3. tdistinct
  4. 25
  5. false
  6. false
  7. false
  8. false
  9. Foo
  10. '''
  11. """
  12. echo "tdistinct"
  13. block tborrowdot:
  14. type
  15. Foo = object
  16. a, b: int
  17. s: string
  18. Bar {.borrow: `.`.} = distinct Foo
  19. var bb: ref Bar
  20. new bb
  21. bb.a = 90
  22. bb.s = "abc"
  23. block tcurrncy:
  24. template Additive(typ: untyped) =
  25. proc `+`(x, y: typ): typ {.borrow.}
  26. proc `-`(x, y: typ): typ {.borrow.}
  27. # unary operators:
  28. proc `+`(x: typ): typ {.borrow.}
  29. proc `-`(x: typ): typ {.borrow.}
  30. template Multiplicative(typ, base: untyped) =
  31. proc `*`(x: typ, y: base): typ {.borrow.}
  32. proc `*`(x: base, y: typ): typ {.borrow.}
  33. proc `div`(x: typ, y: base): typ {.borrow.}
  34. proc `mod`(x: typ, y: base): typ {.borrow.}
  35. template Comparable(typ: untyped) =
  36. proc `<`(x, y: typ): bool {.borrow.}
  37. proc `<=`(x, y: typ): bool {.borrow.}
  38. proc `==`(x, y: typ): bool {.borrow.}
  39. template DefineCurrency(typ, base: untyped) =
  40. type
  41. typ = distinct base
  42. Additive(typ)
  43. Multiplicative(typ, base)
  44. Comparable(typ)
  45. proc `$`(t: typ): string {.borrow.}
  46. DefineCurrency(TDollar, int)
  47. DefineCurrency(TEuro, int)
  48. echo($( 12.TDollar + 13.TDollar )) #OUT 25
  49. block tconsts:
  50. # bug #2641
  51. type MyChar = distinct char
  52. const c:MyChar = MyChar('a')
  53. type MyBool = distinct bool
  54. const b:MyBool = MyBool(true)
  55. type MyBoolSet = distinct set[bool]
  56. const bs:MyBoolSet = MyBoolSet({true})
  57. type MyCharSet= distinct set[char]
  58. const cs:MyCharSet = MyCharSet({'a'})
  59. type MyBoolSeq = distinct seq[bool]
  60. const bseq:MyBoolSeq = MyBoolSeq(@[true, false])
  61. type MyBoolArr = distinct array[3, bool]
  62. const barr:MyBoolArr = MyBoolArr([true, false, true])
  63. # bug #2760
  64. type
  65. DistTup = distinct tuple
  66. foo, bar: string
  67. const d: DistTup = DistTup((
  68. foo:"FOO", bar:"BAR"
  69. ))
  70. # bug #7167
  71. type Id = distinct range[0..3]
  72. proc `<=`(a, b: Id): bool {.borrow.}
  73. var xs: array[Id, bool]
  74. for x in xs: echo x # type mismatch: got (T) but expected 'bool'
  75. # bug #11715
  76. type FooD = distinct int
  77. proc `<=`(a, b: FooD): bool {.borrow.}
  78. for f in [FooD(0): "Foo"]: echo f
  79. block tRequiresInit:
  80. template accept(x) =
  81. static: doAssert compiles(x)
  82. template reject(x) =
  83. static: doAssert not compiles(x)
  84. type
  85. Foo = object
  86. x: string
  87. DistinctFoo {.requiresInit, borrow: `.`.} = distinct Foo
  88. DistinctString {.requiresInit.} = distinct string
  89. reject:
  90. var foo: DistinctFoo
  91. foo.x = "test"
  92. doAssert foo.x == "test"
  93. accept:
  94. let foo = DistinctFoo(Foo(x: "test"))
  95. doAssert foo.x == "test"
  96. reject:
  97. var s: DistinctString
  98. s = "test"
  99. doAssert s == "test"
  100. accept:
  101. let s = "test"
  102. doAssert s == "test"