tstaticgenericparam.nim 620 B

12345678910111213141516171819202122232425
  1. # static types that depend on a generic parameter
  2. block: # issue #19365
  3. var ss: seq[string]
  4. proc f[T](x: static T) =
  5. ss.add($x & ": " & $T)
  6. f(123)
  7. doAssert ss == @["123: int"]
  8. f("abc")
  9. doAssert ss == @["123: int", "abc: string"]
  10. block: # issue #7209
  11. type Modulo[A; M: static[A]] = distinct A
  12. proc `$`[A; M: static[A]](x: Modulo[A, M]): string =
  13. $(A(x)) & " mod " & $(M)
  14. proc modulo[A](a: A, M: static[A]): Modulo[A, M] = Modulo[A, M](a %% M)
  15. proc `+`[A; M: static[A]](x, y: Modulo[A, M]): Modulo[A, M] =
  16. (A(x) + A(y)).modulo(M)
  17. doAssert $(3.modulo(7) + 5.modulo(7)) == "1 mod 7"