Issue #17004 has been updated by jeremyevans0 (Jeremy Evans).


marcandre (Marc-Andre Lafortune) wrote in #note-19:
> jeremyevans0 (Jeremy Evans) wrote in #note-13:
> > Due to Sequel::Dataset#insert's flexible API, it would be hard to support this as a method argument.
> 
> Seems to be that a better API for this is using a block parameter.
> 
> ```ruby
> # need the ID:
> insert(...) do |last_inserted_id|
>   # ...
> end
> 
> # don't need the ID:
> insert(...)
> ```

Sequel::Dataset#insert already accepts a block, which can be used to iterate over rows returned from the insert statement (when using INSERT RETURNING).

> 
> Another possibility is to implement a lazy return value, something like:
> 
> ```ruby
> class LazyID
>   def to_i
>     # get the ID
>   end
> end
> 
> def insert(...)
>   # ...
>   LazyID.new(self)
> end
> ```

In general, this approach requires additional allocation in the case where you are using the return value, decreasing performance.  In this particular case, it's not possible, because you would probably lose access to the database connection used to insert the record before calling `LazyID#to_i`, and that database connection is needed to get the value.

----------------------------------------
Feature #17004: Provide a way for methods to omit their return value
https://bugs.ruby-lang.org/issues/17004#change-86415

* Author: shyouhei (Shyouhei Urabe)
* Status: Open
* Priority: Normal
----------------------------------------
In ruby, it often is the case for a method's return value to not be used by its caller.  Even when a method returns something meaningful, its caller is free to ignore it.

Why not provide a way for a method to know if its return value is needed or not?  That adds a room for methods to be optimized, by for instance skipping creation of complex return values.

The following pull request implements `RubyVM.return_value_is_used?` method, which does that: https://github.com/ruby/ruby/pull/3271



-- 
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>