tcast.nim 640 B

1234567891011121314151617181920212223242526272829303132
  1. discard """
  2. output: '''
  3. Hello World
  4. Hello World'''
  5. joinable: false
  6. """
  7. type MyProc = proc() {.cdecl.}
  8. type MyProc2 = proc() {.nimcall.}
  9. type MyProc3 = proc() #{.closure.} is implicit
  10. proc testProc() = echo "Hello World"
  11. template reject(x) = doAssert(not compiles(x))
  12. proc callPointer(p: pointer) =
  13. # can cast to proc(){.cdecl.}
  14. let ffunc0 = cast[MyProc](p)
  15. # can cast to proc(){.nimcall.}
  16. let ffunc1 = cast[MyProc2](p)
  17. # cannot cast to proc(){.closure.}
  18. reject: cast[MyProc3](p)
  19. ffunc0()
  20. ffunc1()
  21. callPointer(cast[pointer](testProc))
  22. reject: discard cast[enum](0)
  23. proc a = echo "hi"
  24. reject: discard cast[ptr](a)