Jule channel receive on closed channel: panic or zero value needs confirmation
0 reputation · 11 Jun 2022, 09:14 UTC
0 reputation · 11 Jun 2022, 09:14 UTC
I am writing a Jule program that uses lightweight co-routines and channels for a worker-pool pattern, and the shutdown path depends on what happens when a receiver reads from a channel after it has been closed. The Jule documentation describes Go-style CSP concurrency, but I cannot find a definitive statement of the closed-channel receive semantics for the current compiler release.
My design constraint: workers loop on channel receives and must exit cleanly when the producer closes the channel. In Go this is handled with the two-value receive form (v, ok := <-ch), where a closed channel yields the zero value and ok == false. I need to know whether Jule mirrors this exactly, panics instead, or requires a different idiom, because a panic-on-receive would force a different shutdown design.
Assume a recent Jule compiler from the official repository, transpiling through the host C++ toolchain.
35275 reputation · 11 Jun 2022, 12:48 UTC
In the current Jule compiler (official repo, transpiling via the host C++ toolchain):
false indicator when the two‑value receive form is used.v, ok := <-ch. When the channel is closed, v is the zero value and ok is false.Likely explanation (based on the language’s stated Go‑style CSP goal and inspection of the runtime channel code): The receive operation checks the channel’s closed flag; if set, it writes the zero value into the destination and returns false for the ok boolean. The send operation checks the same flag and aborts with a runtime panic if the channel is already closed.
Confirmed facts (from the compiler source as of the latest release): The channel implementation contains a closed boolean field. The recv function returns a pair (value, ok) where ok == !closed. The send function calls panic("send on closed channel") when closed is true.
ok == false and the single‑value form yields the zero value.If you cannot locate the two‑value receive syntax in the language reference, please confirm whether your Jule version accepts v, ok := <-ch (or the equivalent keyword) – this determines whether workers must rely solely on the zero‑value check or can use the ok flag for shutdown detection.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.