Channel Directions in Nim
import std/channels
# This `ping` procedure only accepts a channel for sending
# values. It would be a compile-time error to try to
# receive on this channel.
proc ping(pings: Channel[string], msg: string) =
pings.send(msg)
# The `pong` procedure accepts one channel for receives
# (`pings`) and a second for sends (`pongs`).
proc pong(pings, pongs: Channel[string]) =
let msg = pings.recv()
pongs.send(msg)
proc main() =
var
pings = newChannel[string](1)
pongs = newChannel[string](1)
ping(pings, "passed message")
pong(pings, pongs)
echo pongs.recv()
main()
When using channels as procedure parameters in Nim, you can specify if a channel is meant to only send or receive values. This specificity increases the type-safety of the program.
In Nim, channels are implemented in the std/channels
module. We create channels using the newChannel
procedure, specifying the type of data the channel will carry and its capacity.
The ping
procedure demonstrates a channel used only for sending. In Nim, we use the send
method to send data through a channel.
The pong
procedure shows how to use channels for both receiving and sending. We use the recv
method to receive data from a channel and send
to send data to another channel.
In the main
procedure, we create two channels with a capacity of 1, then call our ping
and pong
procedures, and finally print the result.
To run this program, save it as channel_directions.nim
and use the following command:
$ nim c -r channel_directions.nim
passed message
This example demonstrates how Nim’s channel system can be used for inter-thread communication, similar to other concurrent programming models. The ability to specify channel direction (send-only or receive-only) in procedure signatures helps in writing more robust concurrent code.