async makes generics instantiate only once #4856

Closed
nigredo-tori opened this Issue Oct 5, 2016 · 1 comment

Projects

None yet

2 participants

@nigredo-tori
Contributor
nigredo-tori commented Oct 5, 2016 edited

(Possibly related to #3568, which I cannot reproduce)
Adding async macro to a generic proc makes it only instantiate once, but behave like it was instantiated multiple times. For example:

import asyncdispatch

proc say[T](t: T): Future[void] {.async.} =
  echo $t

waitFor(say(123))
waitFor(say("abc"))

Output:

123
139977063424120

The following workaround seems to work:

proc say[T](t: T): Future[void] =
  proc impl() {.async.} =
    echo $t

  impl()
@nigredo-tori
Contributor

Actually, the workaround doesn't work for more substantial cases. Seems like async runs before the outer proc is instantiated, so it breaks somewhere on encountering a type parameter.
In my case I had to make the proc non-generic, changing all generic values to ref objects with corresponding casts and wrapping other generic procs accordingly.

@Araq Araq added a commit that closed this issue Oct 20, 2016
@Araq Araq fixes #4856 bae3baf
@Araq Araq closed this in bae3baf Oct 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment