tparamsindefault.nim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. discard """
  2. output: '''
  3. @[1, 2, 3]@[1, 2, 3]
  4. a
  5. a
  6. 1
  7. 3 is an int
  8. 2 is an int
  9. miau is a string
  10. f1 1 1 1
  11. f1 2 3 3
  12. f1 10 20 30
  13. f2 100 100 100
  14. f2 200 300 300
  15. f2 300 400 400
  16. f3 10 10 20
  17. f3 10 15 25
  18. true true
  19. false true
  20. world
  21. '''
  22. """
  23. template reject(x) =
  24. assert(not compiles(x))
  25. block:
  26. # https://github.com/nim-lang/Nim/issues/7756
  27. proc foo[T](x: seq[T], y: seq[T] = x) =
  28. echo x, y
  29. let a = @[1, 2, 3]
  30. foo(a)
  31. block:
  32. # https://github.com/nim-lang/Nim/issues/1201
  33. proc issue1201(x: char|int = 'a') = echo x
  34. issue1201()
  35. issue1201('a')
  36. issue1201(1)
  37. # https://github.com/nim-lang/Nim/issues/7000
  38. proc test(a: int|string = 2) =
  39. when a is int:
  40. echo a, " is an int"
  41. elif a is string:
  42. echo a, " is a string"
  43. test(3) # works
  44. test() # works
  45. test("miau")
  46. block:
  47. # https://github.com/nim-lang/Nim/issues/3002 and similar
  48. proc f1(a: int, b = a, c = b) =
  49. echo "f1 ", a, " ", b, " ", c
  50. proc f2(a: int, b = a, c: int = b) =
  51. echo "f2 ", a, " ", b, " ", c
  52. proc f3(a: int, b = a, c = a + b) =
  53. echo "f3 ", a, " ", b, " ", c
  54. f1 1
  55. f1(2, 3)
  56. f1 10, 20, 30
  57. 100.f2
  58. 200.f2 300
  59. 300.f2(400)
  60. 10.f3()
  61. 10.f3(15)
  62. reject:
  63. # This is a type mismatch error:
  64. proc f4(a: int, b = a, c: float = b) = discard
  65. reject:
  66. # undeclared identifier
  67. proc f5(a: int, b = c, c = 10) = discard
  68. reject:
  69. # undeclared identifier
  70. proc f6(a: int, b = b) = discard
  71. reject:
  72. # undeclared identifier
  73. proc f7(a = a) = discard
  74. block:
  75. proc f(a: var int, b: ptr int, c = addr(a)) =
  76. echo addr(a) == b, " ", b == c
  77. var x = 10
  78. f(x, addr(x))
  79. f(x, nil, nil)
  80. block:
  81. # https://github.com/nim-lang/Nim/issues/1046
  82. proc pySubstr(s: string, start: int, endd = s.len()): string =
  83. var
  84. revStart = start
  85. revEnd = endd
  86. if start < 0:
  87. revStart = s.len() + start
  88. if endd < 0:
  89. revEnd = s.len() + endd
  90. return s[revStart .. revEnd-1]
  91. echo pySubstr("Hello world", -5)