From: "tenderlovemaking (Aaron Patterson) via ruby-core" <ruby-core@...>
Date: 2022-12-30T22:51:48+00:00
Subject: [ruby-core:111545] [Ruby master Bug#19278] Constructing subclasses of Data with positional arguments

Issue #19278 has been updated by tenderlovemaking (Aaron Patterson).

Status changed from Feedback to Rejected

zverok (Victor Shepelev) wrote in #note-12:
> > I don't really understand this example. The initialize works the same way whether you use a Struct or not
> 
> It is not.
>
> 1. Both 3.2+ `Struct` and `Data` make a promise that **no other class makes** and is **non-trivial to implement**: if you have Struct/Data-derived class `C` with member `x`, then both `C.new(x: 1)` and `C.new(1)` work.

Your example didn't demonstrate this difference.

> 2. These promises are implemented differently (because different people worked on them :shrug:): In `Struct`, the `#initialize` handles "unify positional and keywords", in `Data`, `.new` handles.
> 3. This leads to different cost/benefit outcome: in `Struct`, it is "like in other classes" but **redefining `#initialize` is non-trivial**, in `Data`, it is "one interesting quirk", but **redefining `#initialize` is trivial**.

I really don't understand point 3.  As I showed in the original post as well as as other examples, I cannot "trivially" define an initialize as I would with normal Ruby classes.

> Can you please provide a small realistic example of how would you like to define your Struct or Data-inherited class' `#initialize` to **not break this contract** and achieve the goal you were trying to achieve? We can proceed from there to maybe come to some compromise or better understand each other's points.

To be honest, I don't really care anymore.  The problems with `new` on `Data` seem quite clear to me, but perhaps I am wrong.  Struct seems to behave in the way I would expect, so I will just use that (or write regular POROs).

Thanks for the feedback.

----------------------------------------
Bug #19278: Constructing subclasses of Data with positional arguments
https://bugs.ruby-lang.org/issues/19278#change-100909

* Author: tenderlovemaking (Aaron Patterson)
* Status: Rejected
* 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/