What will be the result of following code execution?
object Main extends App {
  def quiz = {
    val result = 5
       + 6
  }
  println(quiz)
}
Explanation
The function will be interpreted as following:

  def quiz = {
    val result = 5;
    + 6
  }
The result will be last line: +6
Theory
  • One issue with Scala’s semicolon convention is how to write expressions that span several lines. For instance
    someLongExpression
    + someOtherExpression
    
    would be interpreted as two expressions:
    someLongExpression;
    + someOtherExpression
    
    To overcome this problem you could write the multi-line expression in parentheses, because semicolons are never inserted inside (...):
    (someLongExpression
    + someOtherExpression)
    
    Source: Coursera: Blocks and lexical scope

Shouldn't be there also a warning about local variable `result` not being used?

2020 Feb 15, 5:10:40 PM

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Scala
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести