tags:

views:

150

answers:

5

Does anyone know where the listing for levels of operator precedence for D version 1.0 are located on line?

+2  A: 

See the D 1.0 page on Expressions.

Order Of Evaluation

The following binary expressions are evaluated in strictly left-to-right order:

CommaExpression, OrOrExpression, AndAndExpression

The following binary expressions are evaluated in an implementation-defined order:

AssignExpression, OrExpression, XorExpression, AndExpression, CmpExpression, ShiftExpression, AddExpression, CatExpression, MulExpression, function parameters

It is an error to depend on order of evaluation when it is not specified. For example, the following are illegal:

i = i++;
c = a + (a = b);
func(++i, ++i);

If the compiler can determine that the result of an expression is illegally dependent on the order of evaluation, it can issue an error (but is not required to). The ability to detect these kinds of errors is a quality of implementation issue.

At least, that was the link as mentioned by Walter (D creator) in this mailing list thread.

Mark Rushakoff
Order of evaluation is different to operator precedence.
Bernard
A: 

Google didn't find much, guess D is a bit of a niche language

However, I did stumble on a comment in a thread on converting from C++ to D where D's creator says "Conversion is one reason I kept things like operator precedence the same." So if you can find C++ operator precedence, you should be able to use it for D

Chris Hulan
A: 

Wherever possible, D uses C operator precedence.

Bernard
+1  A: 

There is not, to my knowledge, currently a nice operator precedence table for D. You can look at the page on expressions ( http://www.digitalmars.com/d/1.0/expression.html ) and break down whatever expression you have according to the grammar and figure it out, but obviously that's not as nice or straightforward as a table.

However, C and C++ code is guaranteed to either be valid D code with identical behaviour, or it won't compile. So, as long as the expression that you have would be valid C or C++, you can just use the C/C++ operator precedence table: http://www.cppreference.com/wiki/operator_precedence

So, while there is not currently, unfortunately, a nice operator precedence table for D, as long as you understand C/C++ operator precedence, there really shouldn't be any surprises.

Edit: As for D-specific operators, looking at the expressions page, you've got

Same precedence as other assignment operators

  • >>>=

Probably same precedence as ==

  • in
  • !in
  • is
  • !is

Same precedence as <

  • !<>=
  • !<>
  • <>
  • <>=
  • !>
  • !>=
  • !<
  • !<=

Same precedence as >>

  • >>>

Probably same precedence as *

  • ~

I believe that that's the full list of new D operators. I say "probably" in a couple of those cases because it looks like it's implementation-defined as quoted in Mark Rushakoff's answer (in which case the precedence is very close to the given operator if not identical). However, I'm not sure that it's actually going to matter since it can be pretty hard to mix some of them in a manner which would be at all ambiguous (particularly is and in).

Generally-speaking, if you stick to C/C++ precedence rules, you should be fine. If anything, D will be somewhat more restrictive than C/C++ due to stricter conversion rules and such, so I don't think that you're going to end up with any extra ambiguities. Still, it would probably be a good idea to suggest that an explicit operator precedence table be added to the D documentation.

Jonathan M Davis
Ok, thanks. Is there a list of operators in D that are not in C++?
Winter
+5  A: 

In the page http://www.digitalmars.com/d/1.0/expression.html, the stuff appear higher have lower precedence in general. To get the specific precedence, follow the parser rules.

15.   typeof() typeid() is()
      assert() import() mixin()
      function delegate
      T.x x!y
      variables
      (...) [...]       //(Primary expressions)
      "x" "y"           //(Concatenation between string literals)
14.   . x++ x--
      x() x[]           //(Postfix operators)
13.   & ++x --x * 
      +x -x ! ~ (T).
      new delete
      cast              //(Prefix operators)
12½.  ^^                //(Power. D2 only)
12.   * / %            ///(Multiplicative operators)
11.   + - ~             //(Additive operators)
10.   >> << >>>         //(Bitwise shift)
 9.   == != is !is
      > < >= <=
      !> !< !>= !<=
      <> !<> <>= !<>=
      in !in            //(Comparison operators)
 8.   &                 //(Bitwise AND)
 7.   ^                 //(Bitwise XOR)
 6.   |                 //(Bitwise OR)
 5.   &&                //(Logical AND)
 4.   ||                //(Logical OR)
 3.   ?:                //(Conditional operator)
 2.   op=               //(Assignment operator)
 1½.  ..                //(Slicing. Not really an operator)
 1.   ,                 //(Comma operator)
KennyTM