tfakedependenttypes.nim 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. discard """
  2. output: '''
  3. U[3]
  4. U[(f: 3)]
  5. U[[3]]
  6. '''
  7. """
  8. # https://github.com/nim-lang/Nim/issues/5106
  9. import typetraits
  10. block:
  11. type T = distinct int
  12. proc `+`(a, b: T): T =
  13. T(int(a) + int(b))
  14. type U[F: static[T]] = distinct int
  15. proc `+`[P1, P2: static[T]](a: U[P1], b: U[P2]): U[P1 + P2] =
  16. U[P1 + P2](int(a) + int(b))
  17. var a = U[T(1)](1)
  18. var b = U[T(2)](2)
  19. var c = a + b
  20. echo c.type.name
  21. block:
  22. type T = object
  23. f: int
  24. proc `+`(a, b: T): T =
  25. T(f: a.f + b.f)
  26. type U[F: static[T]] = distinct int
  27. proc `+`[P1, P2: static[T]](a: U[P1], b: U[P2]): U[P1 + P2] =
  28. U[P1 + P2](int(a) + int(b))
  29. var a = U[T(f: 1)](1)
  30. var b = U[T(f: 2)](2)
  31. var c = a + b
  32. echo c.type.name
  33. block:
  34. type T = distinct array[0..0, int]
  35. proc `+`(a, b: T): T =
  36. T([array[0..0, int](a)[0] + array[0..0, int](b)[0]])
  37. type U[F: static[T]] = distinct int
  38. proc `+`[P1, P2: static[T]](a: U[P1], b: U[P2]): U[P1 + P2] =
  39. U[P1 + P2](int(a) + int(b))
  40. var a = U[T([1])](1)
  41. var b = U[T([2])](2)
  42. var c = a + b
  43. echo c.type.name