timplicit_and_explicit.nim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. block: # basic test
  2. proc doStuff[T](a: SomeInteger): T = discard
  3. proc doStuff[T;Y](a: SomeInteger, b: Y): Y = discard
  4. assert typeof(doStuff[int](100)) is int
  5. assert typeof(doStuff[int, float](100, 1.0)) is float
  6. assert typeof(doStuff[int, string](100, "Hello")) is string
  7. proc t[T](x: T; z: int | float): seq[T] = result.add(x & $z)
  8. assert t[string]("Hallo", 2.0) == @["Hallo" & $2.0]
  9. proc t2[T](z: int | float): seq[T] = result.add($z)
  10. assert t2[string](2.0) == @[$2.0]
  11. block: # template test
  12. template someThing[T;Y](a: SomeFloat, b: SomeOrdinal): (T, Y) = (a, b)
  13. assert typeof(someThing[float64, int](1.0, 100)) is (float64, int)
  14. block: # static test
  15. proc t[T](s: static bool) = discard
  16. proc t2[T](s: static string) = discard
  17. t[string](true)
  18. t2[int]("hello")
  19. t2[string]("world")
  20. t2[float]("test222222")
  21. block: #11152
  22. proc f[T](X: typedesc) = discard
  23. f[int](string)
  24. block: #15622
  25. proc test1[T](a: T, b: static[string] = "") = discard
  26. test1[int64](123)
  27. proc test2[T](a: T, b: static[string] = "") = discard
  28. doAssert not (compiles do:
  29. test2[int64, static[string]](123))
  30. block: #4688
  31. proc convertTo[T](v: int or float): T = (T)(v)
  32. discard convertTo[float](1)
  33. block: #4164
  34. proc printStr[T](s: static[string]): T = discard
  35. discard printStr[int]("hello static")
  36. import macros
  37. block: # issue #9040, statics with template, macro, symchoice explicit generics
  38. block: # macro
  39. macro fun[N: static int](): untyped =
  40. newLit 1
  41. const a = fun[2]()
  42. doAssert a == 1
  43. block: # template
  44. template fun[N: static int](): untyped =
  45. 1
  46. const a = fun[2]()
  47. doAssert a == 1
  48. block: # symchoice
  49. proc newSeq[x: static int](): int = 1
  50. template foo: int =
  51. newSeq[2]()
  52. doAssert foo() == 1