[ruby-core:94909] [Ruby master Misc#16157] What is the correct and *portable* way to do generic delegation?
From:
merch-redmine@...
Date:
2019-09-11 21:25:30 UTC
List:
ruby-core #94909
Issue #16157 has been updated by jeremyevans0 (Jeremy Evans).
After discussion with some other committers, I have an alternative approach in https://github.com/ruby/ruby/pull/2449. This allows you to do the following:
```ruby
class Foo
def foo(*args, &block)
bar(*args, &block)
end
pass_keywords :foo if respond_to?(:pass_keywords, true)
end
```
Then if you call `foo` with keywords, keywords will be passed to `bar`. This should allow for more backwards compatible behavior with older versions of Ruby.
Using a modified version of your example:
```ruby
class Proxy < BasicObject
def initialize(target)
@target = target
end
def method_missing(*a, &b)
@target.send(*a, &b) rescue $!
end
pass_keywords(:method_missing) if respond_to?(:pass_keywords, true)
end
class Test
def noarg() end
def noarg_o(**o) o end
def arg(a) a end
def arg_o(a, **o) [a,o] end
def opt_arg(a=nil) a end
def opt_arg_o(a=nil, **o) [a,o] end
def args(*a) a end
def args_o(*a, **o) [a, o] end
end
e = Object.new
def e.write(*args) $stdout.print("warn! ") end
$stderr = e
opt = {}
hash = {k:42}
proxy = Proxy.new(Test.new)
%i'noarg arg opt_arg args'.each do |m|
[m, :"#{m}_o"].each do |meth|
p ["#{meth}(opt)", proxy.send(meth, opt)]
p ["#{meth}(hash)", proxy.send(meth, hash)]
p ["#{meth}(**hash)", proxy.send(meth, **opt)]
p ["#{meth}(**opt)", proxy.send(meth, **opt)]
p ["#{meth}(hash, **opt)", proxy.send(meth, hash, **opt)]
p ["#{meth}(hash, **hash)", proxy.send(meth, hash, **hash)]
end
end
```
Here's a table describing the differences and warnings (differences in ArgumentError messages are ignored). All differences are due to empty keyword splats not being passed as positional arguments in 2.7, and I would consider them at least undesired behavior in 2.6, if not an outright bug.
```
Code 2.6 2.7-pass_keywords
-----------------------------------------------------------------------------
| noarg(opt) | #<ArgE:(1:0)> | #<ArgE:(1:0)>
| noarg(hash) | #<ArgE:(1:0)> | #<ArgE:(1:0)>
d| noarg(**opt) | #<ArgE:(1:0)> | nil
| noarg(**hash) | #<ArgE:(1:0)> | #<ArgE:unknown keyword: :k>
| noarg(hash, **opt) | #<ArgE:(2:0)> | #<ArgE:(1:0)>
| noarg(hash, **hash) | #<ArgE:(2:0)> | #<ArgE:(1:0)>
w | noarg_o(opt) | opt | opt
w | noarg_o(hash) | hash | hash
| noarg_o(**opt) | opt | opt
| noarg_o(**hash) | hash | hash
| noarg_o(hash, **opt) | #<ArgE:(1:0)> | #<ArgE:(1:0)>
| noarg_o(hash, **hash) | #<ArgE:(1:0)> | #<ArgE:(1:0)>
| arg(opt) | opt | opt
| arg(hash) | hash | hash
w | arg(**opt) | opt | opt
| arg(**hash) | opt | hash
d| arg(hash, **opt) | #<ArgE:(2:1)> | hash
| arg(hash, **hash) | #<ArgE:(2:1)> | #<ArgE:unknown keyword: :k>
| arg_o(opt) | [opt, opt] | [opt, opt]
| arg_o(hash) | [hash, opt] | [hash, opt]
w | arg_o(**opt) | [opt, opt] | [opt, opt]
w | arg_o(**hash) | [hash, opt] | [hash, opt]
| arg_o(hash, **opt) | [hash, opt] | [hash, opt]
| arg_o(hash, **hash) | [hash, hash] | [hash, hash]
| opt_arg(opt) | opt | opt
| opt_arg(hash) | hash | hash
d| opt_arg(**opt) | opt | nil
| opt_arg(**hash) | hash | hash
d| opt_arg(hash, **opt) | #<ArgE:(2:0..1)>| hash
| opt_arg(hash, **hash) | #<ArgE:(2:0..1)>| #<ArgE:unknown keyword: :k>
w | opt_arg_o(opt) | [nil, opt] | [nil, opt]
w | opt_arg_o(hash) | [nil, hash] | [nil, hash]
| opt_arg_o(**opt) | [nil, opt] | [nil, opt]
| opt_arg_o(**hash) | [nil, hash] | [nil, hash]
| opt_arg_o(hash, **opt) | [hash, opt] | [hash, opt]
| opt_arg_o(hash, **hash) | [hash, hash] | [hash, hash]
| args(opt) | [opt] | [opt]
| args(hash) | [hash] | [hash]
d| args(**opt) | [opt] | []
| args(**hash) | [hash] | [hash]
d| args(hash, **opt) | [hash, opt] | [hash]
| args(hash, **hash) | [hash, hash] | [hash, hash]
w | args_o(opt) | [[], opt] | [[], opt]
w | args_o(hash) | [[], hash] | [[], hash]
| args_o(**hash) | [[], opt] | [[], opt]
| args_o(**opt) | [[], opt] | [[], opt]
| args_o(hash, **opt) | [[hash], opt] | [[hash], opt]
| args_o(hash, **hash) | [[hash], hash] | [[hash], hash]
```
Every time a warning is issued, behavior is the same in 2.7 as in 2.6. For each warning, here will be the behavior in 3.0:
```
Code 2.6 & 2.7 3.0
-------------------------------------------------------
w | noarg_o(opt) | opt | #<ArgE:(1:0)>
w | noarg_o(hash) | hash | #<ArgE:(1:0)>
w | arg(**opt) | opt | #<ArgE:(0:1)>
w | arg_o(**opt) | [opt, opt] | #<ArgE:(0:1)>
w | arg_o(**hash) | [hash, opt] | #<ArgE:(0:1)>
w | opt_arg_o(opt) | [nil, opt] | [opt, opt]
w | opt_arg_o(hash) | [nil, hash] | [hash, opt]
w | args_o(opt) | [[], opt] | [[opt], opt]
w | args_o(hash) | [[], hash] | [[hash], opt]
```
----------------------------------------
Misc #16157: What is the correct and *portable* way to do generic delegation?
https://bugs.ruby-lang.org/issues/16157#change-81522
* Author: Dan0042 (Daniel DeLorme)
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
With the keyword argument changes in 2.7 we must now specify keyword arguments explicitly when doing generic delegation. But this change is not compatible with 2.6, where it adds an empty hash to the argument list of methods that do not need/accept keyword arguments.
To illustrate the problem:
```ruby
class ProxyWithoutKW < BasicObject
def initialize(target)
@target = target
end
def method_missing(*a, &b)
@target.send(*a, &b)
end
end
class ProxyWithKW < BasicObject
def initialize(target)
@target = target
end
def method_missing(*a, **o, &b)
@target.send(*a, **o, &b)
end
end
class Test
def args(*a) a end
def arg(a) a end
def opts(**o) o end
end
# 2.6 2.7 3.0
ProxyWithoutKW.new(Test.new).args(42) # [42] [42] [42] ok
ProxyWithoutKW.new(Test.new).arg(42) # 42 42 42 ok
ProxyWithoutKW.new(Test.new).opts(k: 42) # {:k=>42} {:k=>42} +warn [{:k=>42}] incompatible with >= 2.7
ProxyWithKW.new(Test.new).args(42) # [42, {}] [42] [42] incompatible with <= 2.6
ProxyWithKW.new(Test.new).arg(42) # error 42 42 incompatible with <= 2.6
ProxyWithKW.new(Test.new).opts(k: 42) # {:k=>42} {:k=>42} +warn {:k=>42} must ignore warning? cannot use pass_positional_hash in 2.6
```
I don't know how to solve this, so I'm asking for the **official** correct way to write portable delegation code. And by **portable** I mean code that can be used in gems that target ruby 2.6 and above.
--
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>