[ruby-core:109779] [Ruby master Feature#18982] Add an `exception: false` argument for Queue#push, Queue#pop, SizedQueue#push and SizedQueue#pop
From:
"byroot (Jean Boussier)" <noreply@...>
Date:
2022-08-30 00:15:03 UTC
List:
ruby-core #109779
Issue #18982 has been updated by byroot (Jean Boussier). > but nil can be problematic? This was discussed in #18774: - `#pop` on closed queue already returns `nil` with no exception. `Queue.new.tap(&:close).pop # => nil` - `nil` make sense because you can use it in a loop such as `while item = queue.pop`. - There's not really any other value that make sense. Overall it's really not that complicated to handle the various cases in which nil is returned. I have an real world example here: https://github.com/Shopify/statsd-instrument/blob/0af8a1e2e74fc24d1de8088ee995940033c1fe42/lib/statsd/instrument/batched_udp_sink.rb#L111-L138. ---------------------------------------- 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-99015 * 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: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>