| 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
-
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
-
- braces {} are not needed for simple expressions
-
no-arg function
-
can be called with or without parenthesis () - as fn or fn()
-
means call-by-name parameters look like variables
-
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()
-
-
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





