ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: matz@ruby.or.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:97779] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass?
Date: Fri, 10 Apr 2020 07:17:15 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-84995.20200410071714.182@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-6087.20120226060237.182@ruby-lang.org

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/

  parent reply	other threads:[~2020-04-10  7:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-84995.20200410071714.182@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).