We've already discussed the theory behind operators in Ceylon, but I missed out on giving you guys an actual list of them. In compiling this list of operators, we've tried to select operators that make code easier to read instead of harder to read. (Ceylon is supposed to be a language that eschews ASCII-art.) There are a couple of operators in the list that I have doubts about, especially the format operator, $, that is used to format an object to a string.
Operator precedence
Ceylon has more operators than Java, and they're somewhat more flexible, due to operator polymorphism, so we decided to categorize the operators into layers
, to make it easier to come up with a consistent set of precedences. According to the language spec:
There are 15 distinct operator precedence levels, but these levels are arranged into layers in order to make them easier to predict.Within each layer, postfix operators have a higher precedence than prefix operators, and prefix operators have a higher precedence than binary operators. There is a single exception to this principal: the binary exponentiation operator ** has a higher precedence than the prefix operators + and -. The reason for this is that the following expressions should be equivalent:
- Operators in layer 1 produce, transform, and combine values.
- Operators in layer 2 compare or predicate values, producing a Boolean result.
- Operators in layer 3 are logical operators that operate upon Boolean arguments to produce a Boolean value.
- Operators in layer 4 perform assignment.
-x**2 //means -(x**2)0 - x**2 //means 0 - (x**2)
Of course, the traditional rules that:
-
multiplicative
operators like *, /, and % have a higher precedence thanadditive
operators like + and -, and -
conjunctive
operators like && and & have a higher precedence thandisjunctive
operators like ||, | and ^
are also respected.
In most cases, these rules result in the same relative operator precedences that you find in C, Java, and C#. But there is one exception to be aware of. The logical not operator ! has a very high precedence in C, but a very low precedence in Ceylon (since it belongs to layer 3).
Now, finally, here's the list, omitting compound assignment operators like +=. (This list is not categorized in order of precedence.)
Basic invocation and assignment operators
- Select: lhs.member
- Invoke: lhs(x,y,z), or lhs { x=x; y=y; z=z; }
- Assign: lhs := rhs
- Format: $rhs
Equality and comparison operators
- Identical: lhs === rhs
- Equals: lhs == rhs
- Not equals: lhs != rhs
- Compare: lhs <=> rhs
- Smaller: lhs < rhs
- Larger: lhs > rhs
- Small as: lhs <= rhs
- Large as: lhs >= rhs
- In: lhs in rhs
- Is: lhs is Rhs
- Satisfies: Lhs satisfies Rhs
Logical operators
- Not: !rhs
- Conditional or: lhs||rhs
- Conditional and: lhs&&rhs
Operators for handling null values
- Exists: lhs exists
- Nonempty: lhs nonempty
- Default: lhs ? rhs
- Nullsafe select: lhs?.member
Correspondence and sequence operators
- Lookup: lhs[index]
- Nullsafe lookup: lhs?[index]
- Subrange: lhs[x..y]
- Upper range: lhs[x...]
- Spread member: lhs[].member
Operators for creating objects
- Range: lhs .. rhs
- Entry: lhs -> rhs
Arithmetic operators
- Successor: ++rhs
- Predecessor: --rhs
- Increment: lhs++
- Decrement: lhs--
- Positive: +rhs
- Negative: -rhs
- Sum: lhs + rhs
- Difference: lhs - rhs
- Product: lhs * rhs
- Quotient: lhs / rhs
- Remainder: lhs % rhs
- Power: lhs ** rhs
Slotwise operators
- Complement: ~rhs
- Or: lhs | rhs
- And: lhs & rhs
- Exclusive or: lhs ^ rhs
- Complement in: lhs ~ rhs
Note that the slotwise operators apply to Sets as well as binary strings, as briefly discussed here. So set union is written U|V, set intersection U&V, and set complement U~V.