Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89985 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92809 invoked from network); 4 Jan 2016 09:39:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Jan 2016 09:39:41 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.182 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.160.182 mail-yk0-f182.google.com Received: from [209.85.160.182] ([209.85.160.182:34888] helo=mail-yk0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 15/02-01776-BDD3A865 for ; Mon, 04 Jan 2016 04:39:40 -0500 Received: by mail-yk0-f182.google.com with SMTP id x67so241548382ykd.2 for ; Mon, 04 Jan 2016 01:39:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=iVXQxW/19ojfvz9lFrkYahtbzNABlc2vvOqWpvAxx04=; b=KIoVkjyhOdFzhg0O+w4qp18IZtUNzTAEbXBF0aMmnBSFAdjtY+N9h5tlnASkPVNB8N aN8D430vUC70p7B6rFXUNG8nuMMcakpaK1cpI1tXhJvNcpelJlE/UbTj31O3/+dign6R Vf7otuaX1g/o08vr45PUX/9ZfkLRTfzRIF7aysS2PHcO3IYTlHC3eORGsePvGo/FZYHF uy3njumTVD5Tu/WmmFlkhKHrUFpUKxoLBEfqWFIjF5uWW/GYkX1qAvOeN6DuUk29N8L1 c1RVKn9/PEGZK1cqb2+6Ku92w+g+uxYSAky6NOJRa+Jw9vXWOFhiFHrkXc61WWK/Zoqp 2Mzw== MIME-Version: 1.0 X-Received: by 10.13.202.194 with SMTP id m185mr70579211ywd.280.1451900377426; Mon, 04 Jan 2016 01:39:37 -0800 (PST) Received: by 10.129.148.70 with HTTP; Mon, 4 Jan 2016 01:39:37 -0800 (PST) In-Reply-To: References: Date: Mon, 4 Jan 2016 10:39:37 +0100 Message-ID: To: Sara Golemon Cc: PHP internals Content-Type: multipart/alternative; boundary=001a11482e783ef18c05287ee628 Subject: Re: [PHP-DEV] [RFC] Differentiate op from assign-op in operator overloading From: nikita.ppv@gmail.com (Nikita Popov) --001a11482e783ef18c05287ee628 Content-Type: text/plain; charset=UTF-8 On Mon, Jan 4, 2016 at 1:37 AM, Sara Golemon wrote: > This is a separate proposal from the userspace operator overloading I > put up for Patricio yesterday and aims to fix what I see as a bug in > our operator overloading implementation (though some may disagree). > > It specifically only seeks to differentiate const operations which > produce a new value from mutations which alter an existing overloaded > object. > > https://wiki.php.net/rfc/assignment-overloading I'd like to provide some context as to why the current implementation works as it does. The basic idea is that, prior to the introduction of operator overloading, primitive operators only worked on types with by-value semantics (or rather: only returned types with by-value semantics). For such types it holds, that $a op= $b is equivalent to $a = $a op $b. We actually transform the latter operation into the former in pass3 optimization. Operator overloading was implemented in such a way that this invariant still holds. To illustrate why keeping this invariant *might* be beneficial, consider this example from the GMP overloading RFC: function powm($base, $exponent, $modulus) { $result = 1; while ($exponent > 0) { if ($exponent % 2 == 1) { $result = $result * $base % $modulus; $exponent--; } $exponent /= 2; $base = ($base * $base) % $modulus; } return $result; } The example appeared in the context that, with operator overloading supported, the above function could be transparently used either with ordinary integers or with overloaded GMP objects. However this would not be true if compound assignment operators would modify the object instead of creating a new one. In this case the argument passed to $exponent would always be GMP(0) after the function finishes running. I expect that this would be somewhat unexpected. Basically the lack of distinction between assign-op and op-and-assign enforces that the object behaves as an immutable value object (at least as far as overloading is concerned). Nikita --001a11482e783ef18c05287ee628--