This Ada code demonstrates a similar concept to the original example, using Ada’s tasking model to simulate channels. Here’s a breakdown of the code:
We define a Channel task type that can store and retrieve strings.
The Channel task uses a String_Array to store the queue items, with a maximum capacity of 2.
The main procedure puts two strings into the channel and then closes it.
We use a loop with a select statement to iterate over the channel’s contents, which is analogous to the range loop in the original example.
The loop exits when there are no more items to retrieve from the channel.
This example shows how to implement a simple producer-consumer pattern in Ada using tasks. While Ada doesn’t have built-in channels like Go, we can achieve similar functionality using tasks and protected objects.
To run this program, save it as range_over_channels.adb and compile it with an Ada compiler:
This example also demonstrates that it’s possible to close a non-empty channel (task in this case) but still have the remaining values be received.