From: "jeremyevans0 (Jeremy Evans)" Date: 2022-08-30T00:42:40+00:00 Subject: [ruby-core:109780] [Ruby master Feature#18982] Add an `exception: false` argument for Queue#push, Queue#pop, SizedQueue#push and SizedQueue#pop Issue #18982 has been updated by jeremyevans0 (Jeremy Evans). shyouhei (Shyouhei Urabe) wrote in #note-4: > +1 for avoiding exceptions but `nil` can be problematic? Because a closed queue would also return `nil` for `pop`. You cannot distinguish if a queue is closed or would just block. We could add another keyword argument for the exception value, and have that value returned instead of raising an exception (the keyword argument would default to `nil`). I'm not sure if it's worth supporting that, but it is a simple approach. ---------------------------------------- Feature #18982: Add an `exception: false` argument for Queue#push, Queue#pop, SizedQueue#push and SizedQueue#pop https://bugs.ruby-lang.org/issues/18982#change-99016 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal ---------------------------------------- This replaces [Feature #18965] Currently these methods raise in three occasions: - `ThreadError(queue empty)` for `#pop` in `nonblock=true` mode, and the operation would block. - `ThreadError(queue full)` for `SizedQueue#push` in `nonblock=true` mode, and the operation would block. - `ClosedQueueError` if trying to `#push` in a closed queue. I see several reasons to prefer a `nil` return value. - Queue is often used in conjunction with threads, so you have to be very careful not to rescue an unrelated `ThreadError`. - Queue if often used for low level code, deep in the stack, so exceptions are costly. I propose that passing `exception: false` would cause the method to return `nil` instead of raising in the three cases listed above. The argument `exception: true` is consistent with various other methods such as `IO#read_nonblock(exception: false)`. -- https://bugs.ruby-lang.org/ Unsubscribe: