Exit Full View

Feather2 / documentation / Precedence.md

Precedence

Precedence determines the order that parts of an expression are evaluated. Consider :

a + b * c

In general, expressions are not evaluated left-to-right. Instead, each operator is ordered by its precedence. If two operators have the same precedence, then they are performed left-to-right.

In the example above * is performed before +, so these are identical :

a + b * c
a + (b * c)
  1. Parentheses
  2. Postfix Unary operators ++ --
  3. Fields/Function/method calls, including with and apply. Array access []
  4. Prefix Unary Operators + - ! ++ --
  5. as as?
  6. Power : ^
  7. Multiplicative : * / ~/ %
  8. Additive + - .. =>
  9. Infix function calls e.g. a myFunction b
  10. Elvis ?:
  11. is !is
  12. Comparisons < > <= >=
  13. Equality == != === !==
  14. Conjunction &&
  15. Disjunction ||
  16. Assignments = += -= *= /= ~/= %=

AFAIK, this ordering is identical to Kotlin.

If you have Commands enabled, then |> (append redirect) is in the Comparison group (together with > and <, which perform redirects). And | (pipe) is Equality group.

PS. I find the terminology confusing. Consider the phrase multiplicative operations have a higher precedence than additive. Yes, they are higher in the list, but the number is lower! To overcome my confusion, I remind myself that 1 is first (i.e. these are ordinals, not cardinals).

If you mix && and ||, I recommend that you ALWAYS include parentheses, even when it isn't strictly needed.

Back to Contents