tspec.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. discard """
  2. output: '''not a var
  3. not a var
  4. a var
  5. B
  6. int
  7. T
  8. int16
  9. T
  10. ref T
  11. 123
  12. 2
  13. 1
  14. @[123, 2, 1]
  15. Called!
  16. merge with var
  17. merge no var'''
  18. """
  19. # Things that's even in the spec now!
  20. proc byvar(x: var int) = echo "a var"
  21. proc byvar(x: int) = echo "not a var"
  22. byvar(89)
  23. let letSym = 0
  24. var varSym = 13
  25. byvar(letSym)
  26. byvar(varSym)
  27. type
  28. A = object of RootObj
  29. B = object of A
  30. C = object of B
  31. proc p(obj: A) =
  32. echo "A"
  33. proc p(obj: B) =
  34. echo "B"
  35. var c = C()
  36. # not ambiguous, calls 'B', not 'A' since B is a subtype of A
  37. # but not vice versa:
  38. p(c)
  39. proc pp(obj: A, obj2: B) = echo "A B"
  40. proc pp(obj: B, obj2: A) = echo "B A"
  41. # but this is ambiguous:
  42. #pp(c, c)
  43. proc takesInt(x: int) = echo "int"
  44. proc takesInt[T](x: T) = echo "T"
  45. proc takesInt(x: int16) = echo "int16"
  46. takesInt(4) # "int"
  47. var x: int32
  48. takesInt(x) # "T"
  49. var y: int16
  50. takesInt(y) # "int16"
  51. var z: range[0..4] = 0
  52. takesInt(z) # "T"
  53. proc gen[T](x: ref ref T) = echo "ref ref T"
  54. proc gen[T](x: ref T) = echo "ref T"
  55. proc gen[T](x: T) = echo "T"
  56. var ri: ref int
  57. gen(ri) # "ref T"
  58. template rem(x) = discard
  59. #proc rem[T](x: T) = discard
  60. rem unresolvedExpression(undeclaredIdentifier)
  61. proc takeV[T](a: varargs[T]) =
  62. for x in a: echo x
  63. takeV([123, 2, 1]) # takeV's T is "int", not "array of int"
  64. echo(@[123, 2, 1])
  65. # bug #2600
  66. type
  67. FutureBase* = ref object of RootObj ## Untyped future.
  68. Future*[T] = ref object of FutureBase ## Typed future.
  69. value: T ## Stored value
  70. FutureVar*[T] = distinct Future[T]
  71. proc newFuture*[T](): Future[T] =
  72. new(result)
  73. proc newFutureVar*[T](): FutureVar[T] =
  74. result = FutureVar[T](newFuture[T]())
  75. proc mget*[T](future: FutureVar[T]): var T =
  76. Future[T](future).value
  77. proc reset*[T](future: FutureVar[T]) =
  78. echo "Called!"
  79. proc merge[T](x: Future[T]) = echo "merge no var"
  80. proc merge[T](x: var Future[T]) = echo "merge with var"
  81. when true:
  82. var foo = newFutureVar[string]()
  83. foo.mget() = ""
  84. foo.mget.add("Foobar")
  85. foo.reset()
  86. var bar = newFuture[int]()
  87. bar.merge # merge with var
  88. merge(newFuture[int]()) # merge no var