On Friday, 8 November 2013 at 12:49:38 UTC, bearophile wrote:
This is something that has come out of a thread I have opened
in D.learn.
In the thread qznc has answered me:
Operator precedence of "." is higher than unary minus.
That's right:
double foo(in double x) {
assert (x >= 0);
return x;
}
void main() {
assert(-1.foo == -1);
}
Is this a good design of the operator precedences? Is this
worth changing/fixing?
Bye,
bearophile
struct Foo
{
int value = 9;
};
struct Bar
{
// implements unary minus
int value = 9;
};
int squareRoot(Bar bar) { return sqrt(bar.value); }
int main() {
Foo foo;
writeln(-foo.value); // prints -9, by first getting the value
and then negating it
Bar bar;
writeln(-bar.squareRoot); // for consistency, this should also
call squareRoot first
// otherwise, the order of operations would depend on whether
the struct has opUnary implemented