tgenericprocvar.nim 733 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. discard """
  2. output: "0false12"
  3. """
  4. # Test multiple generic instantiation of generic proc vars:
  5. proc threadProcWrapper[TMsg]() =
  6. var x: TMsg
  7. stdout.write($x)
  8. #var x = threadProcWrapper[int]
  9. #x()
  10. #var y = threadProcWrapper[bool]
  11. #y()
  12. threadProcWrapper[int]()
  13. threadProcWrapper[bool]()
  14. type
  15. TFilterProc[T,D] = proc (item: T, env:D): bool {.nimcall.}
  16. proc filter[T,D](data: seq[T], env:D, pred: TFilterProc[T,D]): seq[T] =
  17. result = @[]
  18. for e in data:
  19. if pred(e, env): result.add(e)
  20. proc predTest(item: int, value: int): bool =
  21. return item <= value
  22. proc test(data: seq[int], value: int): seq[int] =
  23. return filter(data, value, predTest)
  24. for x in items(test(@[1,2,3], 2)):
  25. stdout.write(x)
  26. stdout.write "\n"