This is as an illustrative example for how Expression Tutor activities could be used in a Scala course. The content on this page may not be understandable for someone without significant prior knowledge.
Scala supports expressions that look familiar to programmers of languages like Java or C. However, behind the scenes these expressions get desugared. You can use the Scala REPL to show these desugarings: enter an expression followed by
"Scala doesn't technically have operator overloading, because it doesn't actually have operators in the traditional sense. Instead, characters such as +, -, *, and / can be used in method names. Thus, when you typed into the Scala interpreter in Step 1, you were actually invoking a method named + on the Int object 1, passing in 2 as a parameter. As illustrated in Figure 3.1, you could alternatively have written 1 + 2 using traditional method invocation syntax,"
Here is an example from the REPL:
Below you can see the desugared expression as a tree. The root node, .+(), is marked with a start. It represents the method invocation. The method is invoked on 1 and receives 2 as argument.
Scala not only desugars binary operators like +, but also unary operators. For example, the unary bitwise complement operator ~ is translated to a call of a method named .
Now draw the corresponding desugared expression tree below. Double click on a node to make it the root of the tree (the root is marked with a star). You do not need all the available nodes. You can delete a node by clicking on it and then clicking on the red circle.
"Another important idea illustrated by this example will give you insight into why arrays are accessed with parentheses in Scala. Scala has fewer special cases than Java. Arrays are simply instances of classes like any other class in Scala. When you apply parentheses surrounding one or more values to a variable, Scala will transform the code into an invocation of a method named apply on that variable. So gets transformed into . Thus accessing an element of an array in Scala is simply a method call like any other. This principle is not restricted to arrays: any application of an object to some arguments in parentheses will be transformed to an apply method call. Of course this will compile only if that type of object actually defines an apply method. So it's not a special case; it's a general rule."
Now assume a variable defined as follows:
Let's desugar an expression that accesses an element in this array. We can use the REPL and hit TAB:
This kind of desugaring is not limited to arrays. Here is another example:
Construct the tree corresponding to the desugared expression. Delete the superfluous node by selecting it and clicking the red circle. Double click on a node to denote it as the root.
"What about for loops that simply perform a side effect without returning anything? Their translation is similar, but simpler than for expressions [...] For instance, the expression:
Here is an example for loop, desugared in the REPL:
The desugared version can be represented as the following expression tree. In the tree, annotate each subexpression with its type. For this, click on each tree node and enter its type. Note that some types, like Int and Int => Unit are available for use with a single click. The node .to() produces a .