
Issue #19278 has been updated by tenderlovemaking (Aaron Patterson). zverok (Victor Shepelev) wrote in #note-8:
BTW, note that before 3.2, for structs it was either `keyword_init: false`, or `keyword_init: true`, not both.
That's why you were able to easily redefine `initialize`: you _knew_ it receives either keywords or positionals, not "both and need to choose". After 3.2, it is different, and I think that people would stumble on that many times (the design decision was made differently, too, unlike Data): ```ruby S = Struct.new(:x) do def initialize(*a, **kwa) puts "a=#{a}, kwa=#{kwa}" end end S.new(1) # a=[1], kwa={} S.new(x: 1) # a=[], kwa={:x=>1} ```
I don't really understand this example. The `initialize` works the same way whether you use a Struct or not: ```ruby S = Struct.new(:x) do def initialize(*a, **kwa) puts "a=#{a}, kwa=#{kwa}" end end S.new(1) # a=[1], kwa={} S.new(x: 1) # a=[], kwa={:x=>1} class K def initialize(*a, **kwa) puts "a=#{a}, kwa=#{kwa}" end end K.new(1) # a=[1], kwa={} K.new(x: 1) # a=[], kwa={:x=>1} ```
I think it will cause a fair share of confusion in completely different ways than the design choice for `Data` did :)
`Data` is confusing for me _because_ it doesn't share the same behavior as a regular class (where Struct does). Eregon (Benoit Daloze) wrote in #note-9:
To clarify:
Also inheriting from a Data class seems kind of an anti/rare pattern.
I meant one should use `Struct.new(...) { ... }` / `Data.define(...) { ... }` and not `class < Struct.new(...)` / `class < Data.define(...)` which is wasteful because it creates an extra class and makes the hierarchy needlessly one level deeper (and also causes an unnamed supercass).
Sure, but making a class that eventually inherits from a class defined via `Struct.new` is something I normally do. I wanted to do the same thing with `Data`, but found I could not because it defines `new` differently. ---------------------------------------- Bug #19278: Constructing subclasses of Data with positional arguments https://bugs.ruby-lang.org/issues/19278#change-100907 * Author: tenderlovemaking (Aaron Patterson) * Status: Feedback * Priority: Normal * ruby -v: ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin22] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I'd expect both of the following subclasses to work, but the subclass that uses positional parameters raises an exception: ```ruby Foo = Data.define class Bar < Foo def initialize foo: p foo end end class Baz < Foo def initialize foo p foo end end Bar.new foo: 1 # Prints 1 Baz.new 1 # Raises ArgumentError ``` I'd expect the subclass that uses positional arguments to work. -- https://bugs.ruby-lang.org/