Re: Power function as operator
| From: | Sherif Ramadan | Date: | Fri, 22 Nov 2013 23:54:28 +0000 |
| Subject: | Re: Power function as operator | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-70304@lists.php.net to get a copy of this message | ||
On Fri, Nov 22, 2013 at 6:37 PM, Nikita Popov <nikita.ppv@gmail.com> wrote:
> On Fri, Nov 22, 2013 at 5:58 PM, Tjerk Meesters <tjerk.meesters@gmail.com
> >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
>
I'm not entirely sure that making it non-associative is a great idea
either, but I have to agree that it does find *some* middle ground.
Right-associative exponent operator didn't seem right to me either.