Creative Reality Pty Ltd

Creating a new reality

  • Increase font size
  • Default font size
  • Decrease font size

Syntax

E-mail Print PDF
Article Index
Syntax
Whole Program Syntax
All Pages

The following article gives an overview of Scala syntax.

Important Rules Summary

  • You can't have a field with the same name as a method
    • In class/object/trait, fields are automatically turned into accessor methods of the same name.

Function Syntax

 def fn( a: Int,  b: String ) = { expression } 
  • args are implicitly val; i.e. they are always read-only

  • parameter types are always required

  • return type is not always required as it can be inferred from expression result

    • required for recursive functions

    • required when using explicit return statement in the function body

 

 def fn( a: Int, b: String ) = expression 
  • braces {} are not needed for simple expressions

 

 

 def fn() = { expression } 
  • no-arg function

  • can be called with or without parenthesis () - as fn or fn()

  • means call-by-name parameters look like variables

     

 

 def field = { expression } 
  • a no-arg function that must be called without parenthesis ()

  • makes object accessor (getter) methods look like object member variables

    • this format seems to be used for compiler generated getters - i.e. you can't call obj.field()

 

 

 def fn( ... )  { expression } 
  • special syntax for procedure - it looks like a normal procedure call

  • doesn't return a value (Unit) - fn has no = sign assigning it to a value

  • procedures are considered to have side-effects and don't return values

  • convention to use () when calling procedures - indicates to the user that there are side-effects



Last Updated on Monday, 10 May 2010 01:24