The topic of operator precedence came up recently in various discussions, and I noticed that our documentation doesn't seem to tell you what the precedence of anything is in REALbasic. Hopefully that will be changing in the near future, but in the meantime -- let's have it out here!
In order from highest precedence to lowest:
| . | The dot operator |
| AddressOf | Delegate creation operator |
| IsA | Type checking operator |
| ^ | Exponentiation operator |
| - | Negation, the unary minus operator |
| Not | Logical not operator |
| * / \ Mod | Multiplication and division arithmetic operators |
| - + | Subtraction and addition arithmetic operators |
| = > < >= <= <> Is | Comparison operators |
| And | Bitwise and logical operator |
| Or Xor | Bitwise and logical operator |
| : | Pair creation operator |
All operators are left-associative except for pairs (:) and exponentiation (^).
Left association means that (foo or bar or baz) will evaluate like ((foo or bar) or baz) instead of (foo or (bar or baz)). Conversely, right association means that (foo : bar : baz) will evaluate like (foo : (bar : baz)) instead of ((foo : bar) : baz).
So when you find yourself wondering why the following doesn't compile, you can consult the operator precedence table to understand it.
if not foo Is Nil then
The reason is because Not has higher precedence than Is, so it evaluates like if ((not foo) is nil). Obviously, "not foo" isn't valid unless foo can evaluate to a boolean or integer. And if it can evaluate to a boolean or an integer, Boolean/Integer Is Nil won't compile, so you'll get an error. Use parens to be explicit about the order you want the items to be evaluated.
This wouldn't be necessary if REALbasic would simply switch to postfix notation for everything.
@Charles -- that sounds wonderful to me. I'm sure no one would take issue with switching to postfix operators. ;-)
How sad is this?
I had a dream a few weeks back that I was writing RB with postfix operators, though my sleeping mind calls it RPN. It was wonderful!
I don't remember "is" coming into it, though.
For some unaccountable reason, I used semicolons between operators.
@Steve -- LoL, so you dream in code too eh?