daniel kullmann
daniel kullmann

Reputation: 14023

compiler error using tryRecv

I have the following Nim program:

import threadpool

var channel: TChannel[string]

proc consumer(channel: TChannel[string]) =
  let (flag,msg) = tryRecv(channel)
  if flag:
    echo msg

channel.open()
spawn consumer(channel)
channel.send("hello")
channel.close()
sync()

When I try to compile it, it gives me this error message:

testchannels.nim(6, 27) Error: type mismatch: got (TChannel[system.string])
but expected one of: 
system.tryRecv(c: var TChannel[tryRecv.TMsg])

I don't understand what the error message is trying to tell me...

Upvotes: 1

Views: 59

Answers (1)

daniel kullmann
daniel kullmann

Reputation: 14023

Ahh, I think I got it now!

The important part of the error message was the var in system.tryRecv(c: var TChannel[tryRecv.TMsg]): tryRecv expects the channel variable to be mutable, which it wasn't in the above code.

The solution is to remove the parameter from the consume proc:

import threadpool

var channel: TChannel[string]

proc consumer() {.gcsafe.} =

  if peek[string](channel) != -1:
    echo recv(channel)

channel.open()
spawn consumer()
channel.send("hello")
channel.close()
sync()

Upvotes: 1

Related Questions