tdistinct.nim 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. discard """
  2. output: '''
  3. tdistinct
  4. 25
  5. '''
  6. """
  7. echo "tdistinct"
  8. block tborrowdot:
  9. type
  10. Foo = object
  11. a, b: int
  12. s: string
  13. Bar {.borrow: `.`.} = distinct Foo
  14. var bb: ref Bar
  15. new bb
  16. bb.a = 90
  17. bb.s = "abc"
  18. block tcurrncy:
  19. template Additive(typ: untyped) =
  20. proc `+`(x, y: typ): typ {.borrow.}
  21. proc `-`(x, y: typ): typ {.borrow.}
  22. # unary operators:
  23. proc `+`(x: typ): typ {.borrow.}
  24. proc `-`(x: typ): typ {.borrow.}
  25. template Multiplicative(typ, base: untyped) =
  26. proc `*`(x: typ, y: base): typ {.borrow.}
  27. proc `*`(x: base, y: typ): typ {.borrow.}
  28. proc `div`(x: typ, y: base): typ {.borrow.}
  29. proc `mod`(x: typ, y: base): typ {.borrow.}
  30. template Comparable(typ: untyped) =
  31. proc `<`(x, y: typ): bool {.borrow.}
  32. proc `<=`(x, y: typ): bool {.borrow.}
  33. proc `==`(x, y: typ): bool {.borrow.}
  34. template DefineCurrency(typ, base: untyped) =
  35. type
  36. typ = distinct base
  37. Additive(typ)
  38. Multiplicative(typ, base)
  39. Comparable(typ)
  40. proc `$`(t: typ): string {.borrow.}
  41. DefineCurrency(TDollar, int)
  42. DefineCurrency(TEuro, int)
  43. echo($( 12.TDollar + 13.TDollar )) #OUT 25
  44. block tconsts:
  45. # bug #2641
  46. type MyChar = distinct char
  47. const c:MyChar = MyChar('a')
  48. type MyBool = distinct bool
  49. const b:MyBool = MyBool(true)
  50. type MyBoolSet = distinct set[bool]
  51. const bs:MyBoolSet = MyBoolSet({true})
  52. type MyCharSet= distinct set[char]
  53. const cs:MyCharSet = MyCharSet({'a'})
  54. type MyBoolSeq = distinct seq[bool]
  55. const bseq:MyBoolSeq = MyBoolSeq(@[true, false])
  56. type MyBoolArr = distinct array[3, bool]
  57. const barr:MyBoolArr = MyBoolArr([true, false, true])
  58. # bug #2760
  59. type
  60. DistTup = distinct tuple
  61. foo, bar: string
  62. const d: DistTup = DistTup((
  63. foo:"FOO", bar:"BAR"
  64. ))