I have a structure like this:
main.nim:
import utils
...
proc send_via_httpclient (url:string): bool =
var client = newHttpClient()
var res = client.getContent(url)
return (res == "ACCEPTED")
proc main(useHttp:bool) =
...
process_info(if useHttp: send_via_httpclient
else: other_method)
...
utils.nim:
...
proc process_info*(sender: (url:string) -> bool) =
var url: string = <generate url here>
if sender(url):
echo "All good"
...
When I try to compile this I get
Error: type mismatch: got <proc (url: string): bool{.gcsafe, locks: <unknown>.}> but expected 'proc (url: string): bool{.gcsafe, locks: 0.}' - lock levels differ
on the process_info line in proc main().
Why's that? I tried to google "lock levels differ" for Nim, but didn't get anywhere.
The type system is really annoying right now with closures. Use explicit
proc send_via_httpclient (url:string): bool {.gcsafe, locks: 0.} =
I think locks are a remnant of an idea to facilitate preventing deadlocks if some code does recursive locking (?) but I think it was quite fully developed and so it's at a stage where it's not really useful but it also can get in your face in strange and annoying ways.
Thanks for this! The pragma {.gcsafe, locks: 0.} works. (I do get a Warning: declared lock level is 0, but real lock level is <unknown> though.)
The big question for me is: Why does the compiler complain in the first place? Why does httpclient do recursive locking? From my understanding it doesn't do anything asynchronously...
Thanks. Good to know.
But seems really strange to me. Does this indicated that higher order functions are not a welcome construct in Nim??? And if so why?
Sorry for these questions, but I am quite a bit baffled...