tparamsindefault.nim 2.1 KB

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