This is copy of Jon Malmaud's go inspired select macro for the Julia programming language. I have made a slight syntax modification, but essentially all the code is his.
This packege is currently not registered and can be installed with:
Pkg.clone("https://github.com/durcan/Select.jl.git")
A select expression is for waiting on multiple communication operations and is of the form:
@select begin
clause1 => body1
clause2 => body2
_ => default_body
end
endWait for multiple clauses simultaneously using an pattern matching syntax, taking a different action depending on which clause is available first. A clause has three possible forms:
event |> valueIfeventis anAbstractChannel, wait for a value to become available in the channel and assigntake!(event)tovalue. ifeventis aTask, wait for the task to complete and assignvaluethe return value of the task.event |< valueOnly suppored forAbstractChannels. Wait for the channel to capabity to store an element, and then callput!(event, value).eventCallswaitonevent, discarding the return value. Usable on any "waitable" events", which include channels, tasks,Conditionobjects, and processes.
If a default branch is provided, @select will check arbitrary choose any event which is ready and execute its body, or will execute default_body if none of them are.
Otherise, @select blocks until at least one event is ready.
For example,
channel1 = Channel()
channel2 = Channel()
task = @task ...
result = @select begin
channel1 |> value => begin
info("Took from channel1")
value
end
channel2 <| :test => info("Put :test into channel2")
task => info("task finished")
end