Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108419 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2280 invoked from network); 6 Feb 2020 22:16:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 6 Feb 2020 22:16:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B52001804DA for ; Thu, 6 Feb 2020 12:29:32 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f44.google.com (mail-ua1-f44.google.com [209.85.222.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 6 Feb 2020 12:29:32 -0800 (PST) Received: by mail-ua1-f44.google.com with SMTP id c7so46603uaf.5 for ; Thu, 06 Feb 2020 12:29:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=+A0bn8WJ16eCEcWpykPtkg5Q4nkYbpZZioFKYy5E/sQ=; b=uXtUYCzgr6k5CA5iZ0iWg4tjWFFjfzc3RhF/W/3nwD4jAKQLbQuPDYRU0eE2hDssZk mT8Af567BDbq4jIUKjOgdWOwKhPrGU+PplMIRc92C3hF9ctjK/iTcjhbyYroMFMiXwXP Rd0o3cgqWx1fMPAEVhNPryFcO3hUfrsx0zTnbCXWPmEd8JpYxC7DEQMaHjEEErrplFIO x6znxfhsaLgsUGrxNwTLG8EcGrnzWBlvj/KWaChGmO6xTKYlaWsp/wg6bWiNp/9FzmCB 8Gz9F0E267VMGbxMAn4uvUhgRq1S8tc2phQ65tZ7JbRFXu/lu6+H3Sk0Wi2ze5+Tz96b l1Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=+A0bn8WJ16eCEcWpykPtkg5Q4nkYbpZZioFKYy5E/sQ=; b=lB9i5hiZLwRS9Htu4H+RNe2Wv/6Kp73TzcxLeIRaMInaHevVdw6Q1eG7oRbTuBRy0T 7cRh8XCXiEjCrMtznOrrGRioJ6h5loMZ5OHA4oRf9jhV/M7lFvhIrt66F9Fq1PfneUqC 1/cxrXOqF8xEGZyobAuv09lBYIfWCMhTUhNd6gGzsGaSQRQDsByvA7V9I9XlBBmcPAp2 1yXPfVfk88zKlRzMe1kkwJPOUn2Rxaw8QAtF53Q1dEJY0ICG2lHcbHxny3SajaiuFEXO 9bqhHPNAgp7VGRxh3fW5mvWWcGUB+aD2Wrjt0QBoE2IKC1krrab2irvZsG66/9ODZvgX kZ/w== X-Gm-Message-State: APjAAAX1uITYAuSAITRPFi1pxdI/VIvBO6yB7HSdFuLbTXljxsvLq2zy PPgUB89UHM62LyfXwfNVxAWEzRW5zng5H1kiZyU= X-Google-Smtp-Source: APXvYqwvUzhRf4AidgSLiemjMs6BT2sOD7eOacWPf6Du9tsd8BOXulS4YMI/J8E0GX+QdvUKGK+VL8jEzBMwhSmVJTo= X-Received: by 2002:ab0:7208:: with SMTP id u8mr2723255uao.68.1581020968299; Thu, 06 Feb 2020 12:29:28 -0800 (PST) MIME-Version: 1.0 References: <00ea01d5d630$b18d4f20$14a7ed60$@gmx.de> <001f01d5d7b3$68c18b10$3a44a130$@gmx.de> <7c915ed1-adcb-473d-a975-acfdbf7b1e33@www.fastmail.com> In-Reply-To: Date: Thu, 6 Feb 2020 15:29:16 -0500 Message-ID: To: Ben Ramsey Cc: Larry Garfield , php internals Content-Type: multipart/alternative; boundary="00000000000032b31c059dee2171" Subject: Re: [PHP-DEV] Operator overloading for userspace objects From: chasepeeler@gmail.com (Chase Peeler) --00000000000032b31c059dee2171 Content-Type: text/plain; charset="UTF-8" On Fri, Jan 31, 2020 at 10:55 AM Ben Ramsey wrote: > > Also, I want to reiterate: Any of these operations MUST be designed to > return a new value, never modify in place. These operators only make sense > on value objects, not service objects, and value objects should be > immutable. > > I completely agree. This was the gist of my earlier comments. > > Maybe we should resurrect discussion of the immutable classes and > properties RFC: https://wiki.php.net/rfc/immutability > > If we add the ability to specify immutability, then we can enforce in the > engine that the left and right operands must be immutable. > > For example: > > public function __add(immutable $left, immutable $right); > > Cheers, > Ben > > Ideally, I don't think the items have to be immutable. Here is a silly use-case: public function __add($left,$right){ $left->operatedOn++; $right->operatedOn++; return $left->value + $right->value; } However, given the nature of operator overloading, I think the users should EXPECT what they pass in will not be changed, unless they explicitly pass by reference. This means we'd have to "change the rules" for operator overloading magic methods, where objects are passed by value unless explicitly passed by reference ( public function __add(&$left, &$right) ). I think that is an even worse idea! So, I think you really have two options. Change the rules so that even objects are passed by value in this specific circumstance, and there is no ability to pass by reference. I still don't like this, because it changes the rules for only a specific scenario, but I think it's a better option than the one above, as well as a better option than allowing mutable objects 100% of the time - although, I'm not totally against spelling out in the documentation "Don't modify the items passed in or you'll get unexpected results!" The other options is the immutability RFC. This doesn't change the rules - it just adds a new rule. I don't see in that RFC, though, anything about the immutable type hints. That's really the only thing that I think is applicable to operator overloading. -- Chase Peeler chasepeeler@gmail.com --00000000000032b31c059dee2171--