The difference here is that the statement “everything is an expression” is made about blocks of code, while “everything is an object” is made about the values in your program.
Code blocks
In Java, there are expressions and expressions. That is, any “block” of code is an expression or expression;
//the if-else-block is a statement whilst (x == 4) is an expression if (x == 4) { foo(); } else { bar(); }
Expressions are of type; no applications; Operators call solely for their side effects .
In scala, every block of code is an expression; that is, it has type:
if (x == 4) foo else bar //has the type lub of foo and bar
It is very important! I cannot stress this enough; this is one of the things scala is good at working with because I can assign a value to an expression.
val x = if (x == 4) foo else bar
Value
By value, I mean that we can refer in the program:
int i = foo();
In Java above, i
not an object - it is a primitive. In addition, I can access the SECONDS
TimeUnit
field, but TimeUnit
is not an object either; it is static (due to the lack of a better phrase). In scala:
val j = 4 Map.empty
As for the language, j
is an object (and I can send methods to it), just like a Map
is a module (or an accompanying object) for type scala.collection.immutable.Map
.
source share