Turns out I was looking for the lambda syntax which is possible with "do". (Although as I hear it's possible in some other way as well)
wait_for server.serve(Port(8080)) do (request: Request) {.async.}:
discard serve_ile(request, "", "todo.json")
This is the "verbose" anonymous proc syntax:
waitFor(server.serve(Port(8080), proc (request: Request) {.async.} =
await request.respond(Http200, "Hello World"))
)
There is the "do notation" feature but many people find this ugly and this feature isn't concrete:
waitFor(server.serve(Port(8080)) do (request: Request) {.async.}:
await request.respond(Http200, "Hello World"))
The one people seem to like the most is sugar.=> , but the current stable version of Nim (1.2.6) does not support putting pragmas like {.async.} on the left hand side of =>. The development version (1.3/1.4) does though, and hopefully this would work:
import sugar
waitFor(server.serve(Port(8080), (request: Request) {.async.} => await request.respond(Http200, "Hello World")))
You can also do the following with =>:
server.serve(Port(8080), (request) {.async.} => await request.respond(Http200, "Hello World"))
and it would probably still work, but this isn't type inference, this simply makes request a generic parameter (as the keyword auto does) and that generic type argument gets inferred by the call to server.serve.
Thanks @Hlaaftana!
I kinda like the do notation. There's a visual distinction between the rest of the arguments and when the callback starts and it takes care of the parenthesis' in the first line.
I wonder how one can call a procedure with multiple callbacks. 😮
Like:
proc animate(duration: int, animation: proc(), completion: proc(completed: bool)) =
animation()
completion(true)
This doesn't work:
animate(5) do ():
echo "animating"
do (completed: bool):
echo $completed
I wonder if it's possible with the do syntax or if not, with any of the syntaxes you described.
That indentation can't possibly be right, right? Try
animate(5) do ():
echo "animating"
do (completed: bool):
echo $completed