From: "tenderlovemaking (Aaron Patterson) via ruby-core" Date: 2022-12-30T01:58:17+00:00 Subject: [ruby-core:111521] [Ruby master Bug#19278] Constructing subclasses of Data with positional arguments Issue #19278 has been updated by tenderlovemaking (Aaron Patterson). Eregon (Benoit Daloze) wrote in #note-3: > It seems hard to solve to me, without losing the significant advantages that @zverok mentions. > > Also inheriting from a Data class seems kind of an anti/rare pattern. > One can of course: > ```ruby > Foo = Data.define(:x, :y) > def custom > end > ... > end > ``` ���� I do this all the time with structs. I assumed `Data.define` would basically just be a read-only `Struct.new` and that's how I got here. zverok (Victor Shepelev) wrote in #note-6: > Yes, the rule "`Data#initialize` accepts only keyword arguments" is unlike the barest "simple class" implementation IMO this would be a fine rule if `new` also only accepted keyword arguments. The automatic coercion from positional to keyword is what got me in to this pickle. This coercion makes subclasses unable to use positional arguments with initialize (a feature which, as I pointed out, regular classes support). I guess it's not really a big deal if I just make all of my subclasses use keyword arguments, and then only ever instantiate them using keyword parameters. But is that the "best practice"? If so, why implement the coercion feature? ---------------------------------------- Bug #19278: Constructing subclasses of Data with positional arguments https://bugs.ruby-lang.org/issues/19278#change-100888 * 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/ ______________________________________________ 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/