Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async procs. Can't use 'await' in templates #3885

Closed
xafizoff opened this issue Feb 19, 2016 · 7 comments
Closed

Async procs. Can't use 'await' in templates #3885

xafizoff opened this issue Feb 19, 2016 · 7 comments
Labels
Async Everything related to Nim's async Standard Library

Comments

@xafizoff
Copy link

Hi.
When I use await keyword in templates, I'm getting the following error:
asyncdispatch.nim(1324, 22) Error: expression 'future' has no type (or is ambiguous)
Code snippet to reproduce:

import asyncnet, asyncdispatch

var clients {.threadvar.}: seq[AsyncSocket]

proc processClient(client: AsyncSocket) {.async.} =
  template do_smth()=
    let line = await client.recvLine()
    if line == "":
      echo "client disconnected"
  while true:
    do_smth()
    for c in clients:
      await c.send(line & "\c\L")

proc serve() {.async.} =
  clients = @[]
  var server = newAsyncSocket()
  server.bindAddr(Port(12345))
  server.listen()

  while true:
    let client = await server.accept()
    clients.add client

    asyncCheck processClient(client)

asyncCheck serve()
runForever()

Nim version: 0.13.0

@dom96
Copy link
Contributor

dom96 commented Feb 19, 2016

You might be able to use yield as a workaround. You won't be able to assign the result like you can with await though.

@xafizoff
Copy link
Author

yield causes the Error: 'yield' only allowed in an iterator.

@dom96
Copy link
Contributor

dom96 commented Feb 20, 2016

Have you tried using an {.immediate.} template?

@xafizoff
Copy link
Author

Sorry, I don't know what was the reason of the yield error, but I cannot reproduce it now. Anyways, I want to read the result of async procedures. So I handle Future objects and use waitFor.

@dom96 dom96 added the Async Everything related to Nim's async label Apr 4, 2016
@ghost
Copy link

ghost commented Sep 17, 2017

@Araq, @dom96 - close?
this template is needed to be a {.dirty.} one, and everything works then

@dom96
Copy link
Contributor

dom96 commented Sep 17, 2017

I don't think so. Dirty shouldn't be necessary.

@ghost
Copy link

ghost commented Sep 18, 2017

@dom96 - this code snippet wouldn't work without a dirty template because line variable is defined in a template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Async Everything related to Nim's async Standard Library
Projects
None yet
Development

No branches or pull requests

3 participants