From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 161521F55B for ; Wed, 13 May 2020 15:14:22 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 8935F120A99; Thu, 14 May 2020 00:13:57 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 4C2E5120A0B for ; Thu, 14 May 2020 00:13:55 +0900 (JST) Received: by filter0081p3las1.sendgrid.net with SMTP id filter0081p3las1-3357-5EBC0EC7-155 2020-05-13 15:14:15.745633991 +0000 UTC m=+2394744.895380893 Received: from herokuapp.com (unknown) by ismtpd0001p1iad1.sendgrid.net (SG) with ESMTP id yjl2iyuxSfKynKvMof2hPQ for ; Wed, 13 May 2020 15:14:15.637 +0000 (UTC) Date: Wed, 13 May 2020 15:14:15 +0000 (UTC) From: daniel@dan42.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 74082 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 6087 X-Redmine-Issue-Author: marcandre X-Redmine-Issue-Assignee: matz X-Redmine-Sender: Dan0042 X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?8sy4RigFvRTdBfCVJrT9zb2J88PC92TMQwdNgaWYaq6MxOFp7SmRVFtD9qHX64?= =?us-ascii?Q?cYsIx7OvOvtrzOe=2FMQlVd1EcMsysRF9mGvOnVs1?= =?us-ascii?Q?+3YGQTMX8cFvoPYDNbzG6Msg=2FG9hylZcyG5utao?= =?us-ascii?Q?RWszx4JPgaHhFHf0Gh+fyszABMv0UayhmWPqm=2Fp?= =?us-ascii?Q?7E=2F4yMvlTR6SSenf5lsqwNR3uq=2F9rnkr37A=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 98321 Subject: [ruby-core:98321] [Ruby master Bug#6087] How should inherited methods deal with return values of their own subclass? X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "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/