From: "bkuhlmann (Brooke Kuhlmann) via ruby-core" Date: 2023-01-02T22:13:34+00:00 Subject: [ruby-core:111582] [Ruby master Bug#19301] Fix Data class to report keyrest instead of rest parameters Issue #19301 has been updated by bkuhlmann (Brooke Kuhlmann). Apologies, I should have used `StructExample = Struct.new :one, :two, keyword_init: true` in the above example. ...but this also highlights a new behavioral change with a `Struct` which doesn't use `keyword_init` but misleadingly accepts positional arguments incorrectly. Should this be logged as a different issue (I'm not sure if the `[[rest]]` problem has been reported yet but will look in the meantime? ---------------------------------------- Bug #19301: Fix Data class to report keyrest instead of rest parameters https://bugs.ruby-lang.org/issues/19301#change-100951 * Author: bkuhlmann (Brooke Kuhlmann) * Status: Open * Priority: Normal * ruby -v: ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin22.2.0] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- ## Overview Hello and Happy New Year. ���� With the new `Data` class, I'm seeing a discrepancy in parameter behavior compared to a `Struct`. I understand the original `Data` [feature](https://bugs.ruby-lang.org/issues/16122) request made design choices to only accept keyword arguments for the `#initialize` method but reporting `[[rest]]` parameters seems misleading to me because it doesn't share the more flexible `Struct#initialize` behavior and would like to request `Data#initialize` answer `[[keyrest]]` for parameters for improved metaprogramming accuracy. ## Steps to Recreate To reproduce, consider the following: ``` ruby DataExample = Data.define :one, :two StructExample = Struct.new :one, :two argument_array = [one: 1, two: 2] argument_hash = {one: 1, two: 2} puts "Data (parameters): #{DataExample.method(:initialize).parameters}" puts "Struct (parameters): #{StructExample.method(:initialize).parameters}" puts "Data (argument hash): #{DataExample[**argument_hash]}" puts "Struct (argument array): #{StructExample[*argument_array]}" puts "Struct (argument hash): #{StructExample[**argument_hash]}" ``` The above will output the following: ``` Data (parameters): [[:rest]] Struct (parameters): [[:rest]] Data (argument hash): # Struct (argument array): #1, :two=>2}, two=nil> Struct (argument hash): # ``` The `Struct` class -- as far as I know -- has always reported `[[rest]]` parameters even though it can accept positional or keyword arguments without error. ...but *this is definitely not the case with* the `Data` class which can be seen when running the following modification to the above: ``` ruby DemoExample[*argument_array] # missing keyword: :two (ArgumentError) ``` The above clearly betrays the `[[rest]]` parameters response (granted a `Struct` is slightly devious too but at least happily accepts positional or keyword arguments). With this in mind, could `Data#initalize` be fixed to at least report `[[keyrest]]` so we'd have a better chance of metaprogramming the correct argument format based on the `#parameters` response for initializing a `Data` instance correctly? Thanks. ����������������� ## Environment `ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin22.2.0]` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/