tundeclaredgenericmacro.nim 864 B

123456789101112131415161718192021222324252627282930313233
  1. import std/[asyncdispatch, httpclient, strutils, json, times]
  2. type
  3. Tree[T] = object
  4. x: T
  5. rr: seq[Tree[T]]
  6. I = Tree[int]
  7. F = Tree[Future[string]]
  8. proc title(t: I): Future[string] {.async,gcsafe.} =
  9. let cli = newAsyncHttpClient()
  10. let c = await cli.getContent("https://jsonplaceholder.typicode.com/todos/" & $t.x)
  11. c.parseJson()["title"].getStr()
  12. proc map[T,U](t: T, f: proc (x: T): U{.async.gcsafe.}): U = #[tt.Error
  13. ^ invalid pragma: async.gcsafe]#
  14. result.x = f(t)
  15. for r in t.rr:
  16. result.rr.add map(r, f)
  17. proc f(t: F, l: int) {.async.} =
  18. echo repeat(' ', l), (await t.x)
  19. for r in t.rr:
  20. await f(r, l+1)
  21. proc asyncMain() {.async.} =
  22. let t = I(x: 1, rr: @[I(x: 2), I(x: 3, rr: @[I(x: 4), I(x: 5)])])
  23. await f(map[I,F](t, title), 0)
  24. let a = now()
  25. waitFor asyncMain()
  26. echo now() - a