Feature #6353

caller-side **

Added by Yusuke Endoh almost 3 years ago. Updated almost 3 years ago.

[ruby-core:44591]
Status:Closed
Priority:Normal
Assignee:Nobuyoshi Nakada

Description

Relating to keyword argument (#5474), there are some requests for
caller-side **.

def foo(k1: 1, k2: 2)
p [k1, k2]
end
h = {k2: "bar"}
foo(k1: "foo", **h) # <== here
#=> ["foo", "bar"]

Marc-Andre explained the use case , and matz agreed with
this feature.

However, it conflicts with power expression when parens are omitted:

foo h # foo(h)? or foo.send("**", h)?

Which should it be interpreted?

Anyway, I have no idea to avoid yacc conflict.
Nobu, could you please try to implement it?

Yusuke Endoh mame@tsg.ne.jp

Associated revisions

Revision 35489
Added by Nobuyoshi Nakada almost 3 years ago

  • parse.y (assoc, parser_yylex): add syntax to splat keyword hash. [Feature #6353]
  • compile.c (compile_array_): generate keyword splat insns.
  • vm.c (m_core_hash_merge_kwd): merge keyword hash into intermediate hash. leftward argument is prior currently.

Revision 35489
Added by Nobuyoshi Nakada almost 3 years ago

  • parse.y (assoc, parser_yylex): add syntax to splat keyword hash. [Feature #6353]
  • compile.c (compile_array_): generate keyword splat insns.
  • vm.c (m_core_hash_merge_kwd): merge keyword hash into intermediate hash. leftward argument is prior currently.

History

#1 Updated by Yukihiro Matsumoto almost 3 years ago

Hi,

In message "Re: [ruby-trunk - Feature #6353][Assigned] caller-side **"
on Tue, 24 Apr 2012 22:04:03 +0900, "mame (Yusuke Endoh)" mame@tsg.ne.jp writes:

|However, it conflicts with power expression when parens are omitted:
|
| foo h # foo(h)? or foo.send("**", h)?
|
|Which should it be interpreted?

It should be interpreted as *, i.e.

foo a # foo(*a)
foo * a # foo.
(a)
foo*a # foo.*(a)

thus

foo h # foo(h)
foo ** h # foo.(h)
foo
h # foo.**(h)

                        matz.

#2 Updated by Nobuyoshi Nakada almost 3 years ago

=begin
Almost implemented, and another question.

What should happen in this case?

def foo(k1: 1)
p k1
end
h = {k1: "bar"}
foo(k1: "foo", **h) # <== conflict

(({k1})) will be (({"foo"})), or (({"bar"}))?
Or an exception should be raised?
=end

#3 Updated by Nobuyoshi Nakada almost 3 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r35489.
Yusuke, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • parse.y (assoc, parser_yylex): add syntax to splat keyword hash. [Feature #6353]
  • compile.c (compile_array_): generate keyword splat insns.
  • vm.c (m_core_hash_merge_kwd): merge keyword hash into intermediate hash. leftward argument is prior currently.

Also available in: Atom PDF