Pow operator precedence

Chad J chadjoan at __spam.is.bad__gmail.com
Sat Jan 14 08:14:18 PST 2012


On 01/13/2012 07:48 AM, bearophile wrote:
> This is the third time I see people trip on power operator precedence:
> http://d.puremagic.com/issues/show_bug.cgi?id=7268
>
> Some people expect this:
> (-10 ^^ 2)
> To be 100 instead of -100
> (Note: Python here uses the same operator precedences.)
>
> Do you think it's worth (and possible) to help D programmers avoid this mistake in their code?
>
> Bye,
> bearophile

I read some of the other responses and... ewwwww.

My only conclusion is that there is no right answer in the precedence 
dichotomy, so my suggestion is to ditch the dichotomy.

Off hand, I'd be comfortable with forbidding such ambiguous expressions 
(in human readability terms; I know the grammar is fine).  It'd be in 
the spirit of things like "if (a = b)".  Parentheses would be required, 
so one must write
(-10)^^2
or
-(10^^2)

I imagine this would make sense for all unary operators colliding with ^^:

++10^^2    // wtf does this do?
...
(++10)^^2  // aha!
or
++(10^^2)  // aha!



More information about the Digitalmars-d mailing list