1

我有以下 Nim 程序:

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()

当我尝试编译它时,它给了我这个错误消息:

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

我不明白错误消息试图告诉我什么......

4

1 回答 1

1

啊,我想我现在明白了!

错误消息的重要部分是varin system.tryRecv(c: var TChannel[tryRecv.TMsg]): tryRecv 期望通道变量是可变的,它不在上面的代码中。

解决方案是从消费过程中删除参数:

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()
于 2015-07-03T15:19:57.837 回答