Scala syntax summary

Type

  • Numeric type: Int, Double (and Byte, Short, Char, Long, Float)
  • Boolean type: with the values true and false
  • String type
  • Function type: ike Int => Int, (Int, Int) => Int

Expression

  • Identifier: such as x, isGoodEnough
  • Literal: like 0, 1.0, ”abc”
  • Function application: like sqrt(x)
  • Operator application: like -x, y + x
  • Selection: like math.abs
  • Conditional expression: like if (x < 0) -x else x
  • Block: like { val x = math.abs(y) ; x * 2 }
  • Anonymous function: like x => x + 1

Definition

  • Function definition: like def square(x: Int) = x * x
  • Value definition: like val y = square(2)

Parameter

  • Call-by-value parameter, like (x: Int): is computed before calling the function
  • Call-by-name parameter, like (y: => Double): is recomputed every time it is accessed

You May Also Like

About the Author: Phuong Ta Thi Thao

Leave a Reply

avatar
  Subscribe  
Notify of