ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:97763] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
       [not found] <redmine.issue-6087.20120226060237.182@ruby-lang.org>
@ 2020-04-09  3:12 ` mame
  2020-04-10  7:17 ` [ruby-core:97779] " matz
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: mame @ 2020-04-09  3:12 UTC (permalink / raw)
  To: ruby-core

Issue #6087 has been updated by mame (Yusuke Endoh).


Recently this ticket was discussed at dev-meeting, and matz changed his mind.  I remember that matz said:

* 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, we need to decide the behavior for each method.

----------------------------------------
Bug #6087: How should inherited methods deal with return values of their own subclass? 
https://bugs.ruby-lang.org/issues/6087#change-84975

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:97779] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
       [not found] <redmine.issue-6087.20120226060237.182@ruby-lang.org>
  2020-04-09  3:12 ` [ruby-core:97763] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass? mame
@ 2020-04-10  7:17 ` matz
  2020-04-10  7:18 ` [ruby-core:97780] " matz
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: matz @ 2020-04-10  7:17 UTC (permalink / raw)
  To: ruby-core

Issue #6087 has been updated by matz (Yukihiro Matsumoto).


I used to think methods should honor subclasses, but I changed my mind that the behavior made things too complex.
So if possible I want to make every method return `Array` instead of instance of a subclass. I just worry about the size of the incompatibility.

Matz.


----------------------------------------
Bug #6087: How should inherited methods deal with return values of their own subclass? 
https://bugs.ruby-lang.org/issues/6087#change-84995

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:97780] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
       [not found] <redmine.issue-6087.20120226060237.182@ruby-lang.org>
  2020-04-09  3:12 ` [ruby-core:97763] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass? mame
  2020-04-10  7:17 ` [ruby-core:97779] " matz
@ 2020-04-10  7:18 ` matz
  2020-04-10 16:44 ` [ruby-core:97811] " eregontp
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: matz @ 2020-04-10  7:18 UTC (permalink / raw)
  To: ruby-core

Issue #6087 has been updated by matz (Yukihiro Matsumoto).


Should we do an experiment in 3.0?

Matz


----------------------------------------
Bug #6087: How should inherited methods deal with return values of their own subclass? 
https://bugs.ruby-lang.org/issues/6087#change-84996

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:97811] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
       [not found] <redmine.issue-6087.20120226060237.182@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2020-04-10  7:18 ` [ruby-core:97780] " matz
@ 2020-04-10 16:44 ` eregontp
  2020-05-07  7:23 ` [ruby-core:98171] " ko1
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: eregontp @ 2020-04-10 16:44 UTC (permalink / raw)
  To: ruby-core

Issue #6087 has been updated by Eregon (Benoit Daloze).


Much like all Enumerable methods return `Array` and (of course) do not copy instance variables, I think Array methods should do the same.

This seems particularly important since Array overrides a few methods from Enumerable for optimization but that should be entirely transparent.
For example, returning a subclass in e.g. Array#map would make it inconsistent with Enumerable#map.

So I'm in favor of no subclass handling here.
We're creating a new instance, and copying the entire state from the receiver doesn't seem reasonable to me.

If people want to keep receiver state like class and @ivars, they can always use mutating methods + #dup if needed.

----------------------------------------
Bug #6087: How should inherited methods deal with return values of their own subclass? 
https://bugs.ruby-lang.org/issues/6087#change-85029

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:98171] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
       [not found] <redmine.issue-6087.20120226060237.182@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2020-04-10 16:44 ` [ruby-core:97811] " eregontp
@ 2020-05-07  7:23 ` ko1
  2020-05-13 15:14 ` [ruby-core:98321] " daniel
  2020-10-23  4:10 ` [ruby-core:100511] " merch-redmine
  6 siblings, 0 replies; 7+ messages in thread
From: ko1 @ 2020-05-07  7:23 UTC (permalink / raw)
  To: ruby-core

Issue #6087 has been updated by ko1 (Koichi Sasada).


Eregon (Benoit Daloze) wrote in #note-14:
> Much like all Enumerable methods return `Array` and (of course) do not copy instance variables, I think Array methods should do the same.

+1

----------------------------------------
Bug #6087: How should inherited methods deal with return values of their own subclass? 
https://bugs.ruby-lang.org/issues/6087#change-85409

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:98321] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
       [not found] <redmine.issue-6087.20120226060237.182@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2020-05-07  7:23 ` [ruby-core:98171] " ko1
@ 2020-05-13 15:14 ` daniel
  2020-10-23  4:10 ` [ruby-core:100511] " merch-redmine
  6 siblings, 0 replies; 7+ messages in thread
From: daniel @ 2020-05-13 15:14 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:100511] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
       [not found] <redmine.issue-6087.20120226060237.182@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2020-05-13 15:14 ` [ruby-core:98321] " daniel
@ 2020-10-23  4:10 ` merch-redmine
  6 siblings, 0 replies; 7+ messages in thread
From: merch-redmine @ 2020-10-23  4:10 UTC (permalink / raw)
  To: ruby-core

Issue #6087 has been updated by jeremyevans0 (Jeremy Evans).


matz (Yukihiro Matsumoto) wrote in #note-13:
> Should we do an experiment in 3.0?

I've added a pull request that modifies the Array methods to return Array instances instead of subclass instances: https://github.com/ruby/ruby/pull/3690


----------------------------------------
Bug #6087: How should inherited methods deal with return values of their own subclass? 
https://bugs.ruby-lang.org/issues/6087#change-88134

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-10-23  4:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-6087.20120226060237.182@ruby-lang.org>
2020-04-09  3:12 ` [ruby-core:97763] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass? mame
2020-04-10  7:17 ` [ruby-core:97779] " matz
2020-04-10  7:18 ` [ruby-core:97780] " matz
2020-04-10 16:44 ` [ruby-core:97811] " eregontp
2020-05-07  7:23 ` [ruby-core:98171] " ko1
2020-05-13 15:14 ` [ruby-core:98321] " daniel
2020-10-23  4:10 ` [ruby-core:100511] " merch-redmine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).