What is the difference between expressions and statements in Scala

I am new to the Scala world and want to know what is the difference between expressions and statements and why if-else is used for an expression and not for statements. And if there is a way to use the operator in if-else?

+8
scala
source share
3 answers

EDIT . As @ Jörg W Mittag noted in the comments, this answer is actually incorrect: statements exist in the Scala spec :

Statements occur as parts of blocks and templates. An operator can be an import, definition or expression, or it can be empty.

I leave the original answer as I think it is still valuable as a practical answer to the OP question.


The original answer

Pedally speaking, there is no such thing as a statement in Scala. There are definitions (e.g. def and class ) and expressions. Everything that will be called an expression in other languages ​​is just an expression.

For example, many people will invoke the following “line,” which prints to the console, a statement:

 println("hello") 

In Scala, this is an expression. For example, you can assign it to the value:

 val x = println("hello") 

In this case, x assigned the value () (pronounced unit ) of type unit .

The "theory" aside, it is usually customary to call an expression that always returns the operator () . This means that the above println("hello") can also be considered a statement.

Back to if/else . Since everything is an expression, this if/else is an expression:

 if (cond) 3 else 5 

but also

 if (cond) println("hello") else println("bye") 

In the last example, however, if/else always returns () (because both println return () ), and so you can call this if/else a if/else .

+9
source share

An expression is all that can be evaluated to get a value. When an operator is evaluated, it causes side effects, but does not come down to value.

A useful rule of thumb is that if you can put it to the right of the assignment, it is an expression.

 val x = (anything legal here must be an expression) 

If you cannot put it on the right side of the assignment, this is probably a statement. For example, none of them is valid, because the bits on the right are operators:

 val x = import scala.collection.mutable // import is a statement val x = def someFunction() = { ... } // Function definition is a statement val x = val y = 42 // Variable assignment is a statement 

Scala, like many functional first languages, has very few “clean” statements. Instead, many of the constructs that people think of (and call them) are expressions that evaluate Unit :

 scala> val x = print("hello world") // a "print statement" is actually an expression x: Unit = () scala> val x = for (i in Array(1, 2, 3)) println("Hello " + i) // for loops are expressions too! x: Unit = () 

Processing the words “if” as expressions is actually very convenient:

 val meal = if (hungry) Some(food) else None 

tl; dr: The technical difference between an operator and an expression is that the expression evaluates the value, but the operator does not. Informally, many people use the term “statement” to mean something that is evaluated only for side effects, but many of these “statements” are actually expressions that are rated as Unit .

+2
source share

Statements are more important and expressions are more functional. If this is what you are looking for. Expressions are similar to assignments and therefore are equivalent to "var", not "val", and they do not print the values, to get the value from them you need println (s "..."). Whereas expressions immediately give meaning and are of type.

+1
source share

All Articles