[ruby-core:98321] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
From:
daniel@...42.com
Date:
2020-05-13 15:14:15 UTC
List:
ruby-core #98321
Issue #6087 has been updated by Dan0042 (Daniel DeLorme).
> * A method that seems to return a new array that is directly related to the receiver, should return an instance of the receiver's class.
> * A method that seems to return a new array that is not directly related to the receiver, should return an Array.
So this is the old thinking?
> I used to think methods should honor subclasses, but I changed my mind that the behavior made things too complex.
And this is the new thinking? In that case +1
If a subclass needs a method to return an instance of the subclass, it can easily and _safely_ opt-in to this behavior (similar to Hash)
```ruby
class A < Array
def select(...)
A.new(super) #or e.g. dup.replace(super) depending on specifics of the subclass
end
end
```
On the other hand returning a subclass by default opens the door to all kinds of complexity and bugs depending on how the subclass is implemented. In particular if it has any state/ivars. `ary.select` is not the same as `ary.dup.select!` in that case.
Is there somewhere a complete list of methods that currently return a subclass?
For Array I think there's only this: drop, drop_while, take, take_while, flatten, uniq, slice
----------------------------------------
Bug #6087: How should inherited methods deal with return values of their own subclass?
https://bugs.ruby-lang.org/issues/6087#change-85560
* Author: marcandre (Marc-Andre Lafortune)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 3.0
* ruby -v: trunk
----------------------------------------
Just noticed that we still don't have a consistent way to handle return values:
```ruby
class A < Array
end
a = A.new
a.flatten.class # => A
a.rotate.class # => Array
(a * 2).class # => A
(a + a).class # => Array
```
Some methods are even inconsistent depending on their arguments:
```ruby
a.slice!(0, 1).class # => A
a.slice!(0..0).class # => A
a.slice!(0, 0).class # => Array
a.slice!(1, 0).class # => Array
a.slice!(1..0).class # => Array
```
Finally, there is currently no constructor nor hook called when making these new copies, so they are never properly constructed.
Imagine this simplified class that relies on `@foo` holding a hash:
```ruby
class A < Array
def initialize(*args)
super
@foo = {}
end
def initialize_copy(orig)
super
@foo = @foo.dup
end
end
a = A.new.flatten
a.class # => A
a.instance_variable_get(:@foo) # => nil, should never happen
```
I feel this violates object orientation.
One solution is to always return the base class (`Array`/`String`/...).
Another solution is to return the current subclass. To be object oriented, I feel we must do an actual `dup` of the object, including copying the instance variables, if any, and calling `initialize_copy`. Exceptions to this would be (1) explicit documentation, e.g. `Array#to_a`, or (2) methods inherited from a module (like `Enumerable` methods for `Array`).
I'll be glad to fix these once there is a decision made on which way to go.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>