Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70303 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98868 invoked from network); 22 Nov 2013 23:37:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Nov 2013 23:37:45 -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.214.176 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.176 mail-ob0-f176.google.com Received: from [209.85.214.176] ([209.85.214.176:48388] helo=mail-ob0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BD/F1-22438-7CAEF825 for ; Fri, 22 Nov 2013 18:37:44 -0500 Received: by mail-ob0-f176.google.com with SMTP id va2so2026842obc.7 for ; Fri, 22 Nov 2013 15:37: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=224IXHXNE3BLe+p+WMZjDlTWLFzgzy7fCHB2Dmjrg4Y=; b=JBc/KI0vGwIXKY0tE5Ar/pV62a+8Lc1jdg+wCUzyKa/64EZpqgi3s1AzYh5XLITPgq gNHlEhtZiBZna34gwDEEhR7KRsgd20AnFLcq57KAymRRtyQp+bkVLvEowC3uM6AzBKYi 5WGOtIkvmXnnK0tR9xtyWAYnsmkRSwr8PQyZkk/ZkJQCAnFYDy7KLuwttUKaGH2WfjHK 17n/jYgQGMd+1+1JkdtZv8uAPAaS2mKMjmnIibGpdzCs0CyOkF4eSVfw9Eh4Rr9sAE+H EJlTI6kXhNVkonlx/c++nipO2NPWbLXxx2sxmgNmZl5tI4KFX3ph/u409J5jrnE0mrld 8i4w== MIME-Version: 1.0 X-Received: by 10.60.133.233 with SMTP id pf9mr13005451oeb.46.1385163459534; Fri, 22 Nov 2013 15:37:39 -0800 (PST) Received: by 10.182.54.112 with HTTP; Fri, 22 Nov 2013 15:37:39 -0800 (PST) In-Reply-To: References: Date: Sat, 23 Nov 2013 00:37:39 +0100 Message-ID: To: Tjerk Meesters Cc: PHP Internals Content-Type: multipart/alternative; boundary=047d7b472078f62bae04ebcc7fd6 Subject: Re: [PHP-DEV] Power function as operator From: nikita.ppv@gmail.com (Nikita Popov) --047d7b472078f62bae04ebcc7fd6 Content-Type: text/plain; charset=ISO-8859-1 On Fri, Nov 22, 2013 at 5:58 PM, Tjerk Meesters wrote: > Hi, > > To challenge myself I had tasked myself to introduce a new operator and > opcode to the language; now that I'm done with it, I wanted to measure the > response on the list to actually get it merged before writing an RFC. > > My work can be found here: > https://github.com/datibbaw/php-src/compare/pow-operator > > It introduces the pow() function as an operator ** (double asterisk), as > can be found in languages such as Python (with perhaps the notable > difference that it's right associative there). > > The logic gets exposed via the ZEND_POW opcode and all the logic that went > into pow() itself is copied into it. The exceptions are that an expression > such as [] ** 2 (squared empty array) will cause a fatal error because the > operands are incompatible, whereas pow([], 2) would give 0. > > Why this operator? Basically because: > 1) it's shorter (the keyboard rejoices). > 2) it's faster (no ZEND_CALL). > 3) it's found in other languages too. > > I've only implemented one of the test suites; there are quite a few for > just one function, but when needed I can add those others as well. > > Btw, changes to vld aren't pushed yet. > > Let me know! > I don't really see a need for this operator in PHP. From my experience pow() isn't some particularly commonly used function, so I don't think it needs its own operator. (But I don't think that adding it would particularly hurt either...) Anyway, if this is added I would strongly recommend to make the operator non-associative. I have no idea how Python came up with the idea to make ** right-associative, which violates basic programming language design rules (all binary non-assignment operators are left-associative) and goes against (at least my) common sense, but now that they made the choice it's likely unwise to deviate from it by making it left-associative in PHP (see ternary operator mess...) So I think making it non-associative makes for a safe middle ground. This will cause 2 ** 3 ** 2 to throw an error and require you to group it as either (2 ** 3) ** 2 or 2 ** (3 ** 3). Less ambiguity. Nikita --047d7b472078f62bae04ebcc7fd6--