Issue #17004 has been updated by soulcutter (Bradley Schaefer).


jeremyevans0 (Jeremy Evans) wrote in #note-13:

> ```ruby
>   def foo(**kw)
>     kw.merge(FORCE_VALUES)
>     bar(**kw)
>   end
> ```
> 
> This code has a bug I've seen new Ruby programmers make.  The bug is that `Hash#merge` returns a new hash, it doesn't modify the existing hash.  This is almost certainly a bug, because there is no reason to call `Hash#merge` without using the return value.  The programmer almost certainly wanted the behavior of `Hash#merge!`.  Basically, `Hash#merge` is a pure function.  We could add a way to mark methods as pure functions (e.g. `Module#pure_function`), and if the method is called with VM_FRAME_FLAG_DISCARDED, Ruby could warn or raise.

What scares me about this is the idea of using interactive debuggers (or even plan-old puts debugging) changing the behavior of the code. You would have to be an expert (primed to think about this behavior) to recognize that simply observing a method means you can't make any assumptions about what happens when you're not observing it. Also, how would you test this behavior?


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

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