tprocvar.nim 822 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. discard """
  2. output: '''
  3. papbpcpdpe7
  4. '''
  5. """
  6. block genericprocvar:
  7. proc foo[T](thing: T) =
  8. discard thing
  9. var a: proc (thing: int) {.nimcall.} = foo[int]
  10. block tprocvar2:
  11. proc pa() {.cdecl.} = write(stdout, "pa")
  12. proc pb() {.cdecl.} = write(stdout, "pb")
  13. proc pc() {.cdecl.} = write(stdout, "pc")
  14. proc pd() {.cdecl.} = write(stdout, "pd")
  15. proc pe() {.cdecl.} = write(stdout, "pe")
  16. const algos = [pa, pb, pc, pd, pe]
  17. var x: proc (a, b: int): int {.cdecl.}
  18. proc ha(c, d: int): int {.cdecl.} =
  19. echo(c + d)
  20. result = c + d
  21. for a in items(algos):
  22. a()
  23. x = ha
  24. discard x(3, 4)
  25. block tprocvars:
  26. proc doSomething(v: int, x: proc(v:int):int): int = return x(v)
  27. proc doSomething(v: int, x: proc(v:int)) = x(v)
  28. doAssert doSomething(10, proc(v: int): int = return v div 2) == 5