tproctypeclass.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import std/assertions
  2. proc main =
  3. iterator closureIter(): int {.closure.} =
  4. yield 1
  5. yield 2
  6. iterator inlineIter(): int {.inline.} =
  7. yield 1
  8. yield 2
  9. proc procNotIter(): int = 1
  10. doAssert closureIter is iterator
  11. doAssert inlineIter is iterator
  12. doAssert procNotIter isnot iterator
  13. doAssert closureIter isnot proc
  14. doAssert inlineIter isnot proc
  15. doAssert procNotIter is proc
  16. doAssert typeof(closureIter) is iterator
  17. doAssert typeof(inlineIter) is iterator
  18. doAssert typeof(procNotIter) isnot iterator
  19. doAssert typeof(closureIter) isnot proc
  20. doAssert typeof(inlineIter) isnot proc
  21. doAssert typeof(procNotIter) is proc
  22. block:
  23. proc fn1(iter: iterator {.closure.}) = discard
  24. proc fn2[T: iterator {.closure.}](iter: T) = discard
  25. fn1(closureIter)
  26. fn2(closureIter)
  27. doAssert not compiles(fn1(procNotIter))
  28. doAssert not compiles(fn2(procNotIter))
  29. doAssert not compiles(fn1(inlineIter))
  30. doAssert not compiles(fn2(inlineIter))
  31. block: # concrete iterator type
  32. proc fn1(iter: iterator(): int) = discard
  33. proc fn2[T: iterator(): int](iter: T) = discard
  34. fn1(closureIter)
  35. fn2(closureIter)
  36. doAssert not compiles(fn1(procNotIter))
  37. doAssert not compiles(fn2(procNotIter))
  38. doAssert not compiles(fn1(inlineIter))
  39. doAssert not compiles(fn2(inlineIter))
  40. proc takesNimcall[T: proc {.nimcall.}](p: T) = discard
  41. proc takesClosure[T: proc {.closure.}](p: T) = discard
  42. proc takesAnyProc[T: proc](p: T) = discard
  43. proc nimcallProc(): int {.nimcall.} = 1
  44. proc closureProc(): int {.closure.} = 2
  45. doAssert nimcallProc is proc {.nimcall.}
  46. takesNimcall(nimcallProc)
  47. doAssert closureProc isnot proc {.nimcall.}
  48. doAssert not compiles(takesNimcall(closureProc))
  49. doAssert nimcallProc isnot proc {.closure.}
  50. doAssert not compiles(takesClosure(nimcallProc))
  51. doAssert closureProc is proc {.closure.}
  52. takesClosure(closureProc)
  53. doAssert nimcallProc is proc
  54. takesAnyProc(nimcallProc)
  55. doAssert closureProc is proc
  56. takesAnyProc(closureProc)
  57. block: # supposed to test that sameType works
  58. template ensureNotRedefine(Ty): untyped =
  59. proc foo[T: Ty](x: T) = discard
  60. doAssert not (compiles do:
  61. proc bar[T: Ty](x: T) = discard
  62. proc bar[T: Ty](x: T) = discard)
  63. ensureNotRedefine proc
  64. ensureNotRedefine iterator
  65. ensureNotRedefine proc {.nimcall.}
  66. ensureNotRedefine iterator {.nimcall.}
  67. ensureNotRedefine proc {.closure.}
  68. ensureNotRedefine iterator {.closure.}
  69. main()