Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Operators  ›  Capstone Project

Operators Are Methods

Operators 12 minIn Scala an operator is an ordinary method written between its operands, the first character of its name fixes its precedence, and names ending in a colon group to the right.

Operators Are Methods

Why does 5.0 max 8.5 work in Scala, with a word used like a plus sign? Because Scala has no special list of operators. An operator is an ordinary method with a short name, and that one idea explains both how operators work and in what order they run.

The rule

a op b is a.op(b); the first character decides precedence.

Writing 2 + 3 really calls a method named + on the number 2:

2 + 3          // the same as 2.+(3)
5.0 max 8.5    // the same as 5.0.max(8.5): 8.5
1 to 4         // the same as 1.to(4): the numbers 1, 2, 3, 4

A method with one parameter can be written between its two operands like this, which is called infix style. Symbolic names such as + are almost always written that way; word names such as max can go either way.

When several operators meet, Scala ranks them by the first character of their name, from lowest to highest:

(any letter)       lowest: max, min, to, until
|
^
&
= !
< >
:
+ -
* / %
(other symbols)    highest

So 2 + 3 * 4 is 14, because * outranks +. And 3 max 2 + 5 is 7, because max begins with a letter and ranks lowest: it means 3 max (2 + 5). Assignment operators such as += sit below everything in the table.

Operators of equal rank run left to right: 10 - 4 - 3 is (10 - 4) - 3, which is 3. The one exception is names ending in a colon. They group to the right and belong to the value on their right, so 1 :: 2 :: Nil means 1 :: (2 :: Nil), a list built from the right.

When the order is not obvious to a reader, add parentheses. They cost nothing and remove the doubt.

The shortcut

To evaluate a mixed expression, rank by first character, then go left to right within a rank. Letters rank last, so word operators like max and to act on the finished arithmetic on each side of them.

Evaluating laps * 400 / 1000.0 max 1.0 with laps = 12Step 1 of 3
  1. Rank the operators

    * and / share the top rank. max starts with a letter, so it goes last.

Drills

Answer fast — trust the rule.

quick drill

What is 2 + 3 * 4?

quick drill

What is 3 max 2 + 5?

quick drill

How does 1 :: 2 :: Nil group?

quick drill

What is 10 - 4 - 3?

quick drill

Which call is exactly what 5.0 max 8.5 means?

Remember

  • An operator is a method: a op b means a.op(b).
  • Rank by first character (letters lowest, * / % above + -), then go left to right; names ending in : group to the right.
  • Trap: word operators like max rank below arithmetic, so add parentheses when in doubt.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainEvaluating by rank

Evaluate 2 * 3 + 10 max 20 - 5 step by step, explaining how Scala decides the order. Then add parentheses to make the same expression obvious to a reader.