tborrow.nim 604 B

123456789101112131415161718192021222324252627282930313233343536
  1. discard """
  2. output: '''4887 true
  3. 0.5'''
  4. """
  5. # test the new borrow feature that works with generics:
  6. proc `++`*[T: int | float](a, b: T): T =
  7. result = a + b
  8. type
  9. DI = distinct int
  10. DF = distinct float
  11. DS = distinct string
  12. proc `++`(x, y: DI): DI {.borrow.}
  13. proc `++`(x, y: DF): DF {.borrow.}
  14. proc `$`(x: DI): string {.borrow.}
  15. proc `$`(x: DF): string {.borrow.}
  16. echo 4544.DI ++ 343.DI, " ", (4.5.DF ++ 0.5.DF).float == 5.0
  17. # issue #14440
  18. type Radians = distinct float64
  19. func `-=`(a: var Radians, b: Radians) {.borrow.}
  20. var a = Radians(1.5)
  21. let b = Radians(1.0)
  22. a -= b
  23. echo a.float64