2. Basic Syntax
❖ Scala is case-sensitive
❖ Class names - For all class names, the first letter should be in Upper
Case.
❖ Method Names - All method names should start with a Lower Case letter.
❖ Program File Name - Name of the program file should exactly match the
object name.
3. Characters are distinguished
according to the following classes
1. Whitespace characters: u0020(space) u0009(tab) u000D(r) u000A(n)
2. Letters: (Unicode categories) lower case letters(Ll), upper case letters(Lu), title-
case letters(Lt), other letters(Lo), letter numerals(Nl) and the two characters
‘$’ and ‘_’
3. Digits: ‘0’ . . . ‘9’
4. Parentheses: ( ) [ ] { }
5. Delimiter characters: ‘ ’ " . ; ,
6. Operator characters: all printable ASCII characters u0020-u007F, mathematical
symbols(Sm) and other symbols(So) except parentheses ([]) and periods
4. Characters are distinguished
according to the following classes
Ex:
val #^ = 1 // legal - two opchars
val # = 1 // illegal - reserved word like class or => or @
val + = 1 // legal - opchar
val &+ = 1 // legal - two opchars
val &2 = 1 // illegal - opchar and letter do not mix arbitrarily
val £2 = 1 // working - £ is part of Sc (Symbol currency) - undefined by spec
val ¬ = 1 // legal - part of Sm
6. ALPHANUMERIC IDENTIFIERS
● An ALPHANUMERIC IDENTIFIERS starts with a letter or underscore,
which can be followed by further letters, digits, or underscores.
● '$' character is a reserved keyword
Legal alphanumeric identifiers:
age, salary, _value, __1_value
Illegal identifiers:
$salary, 123abc, -salary
7. OPERATOR IDENTIFIERS
● An operator identifier consists of one or more operator characters.
● Operator characters are printable ASCII characters such as +, :, ?, ~ or #.
Legal operator identifiers:
+ ++ ::: <?> :>
ps: The Scala compiler will internally "mangle" operator identifiers to turn them into legal Java
identifiers with embedded $ characters. For instance, the identifier :-> would be represented internally
as $colon$minus$greater method. This is consistent with Java anonymous class names.
8. MIXED IDENTIFIERS
Consists of an alphanumeric identifier, which is followed
by an underscore and an operator identifier.
Legal mixed identifiers: unary_+, myvar_=
unary_+ as a method name defines a unary + operator
myvar_= as method name defines an assignment operator.
9. MIXED IDENTIFIERS
Scala will only allow mixed identifier names (containing alphanumerics and punctuation) if
you separate them by _.
Illegal identifiers:
scala> def iszero?(x : Int) = x == 0
<console>:1: error: '=' expected but identifier found.
def iszero?(x : Int) = x == 0
^
Legal identifiers:
scala> def iszero_?(x : Int) = x == 0
iszero_$qmark: (x: Int)Boolean
10. LITERAL IDENTIFIERS
A literal identifier is an arbitrary string enclosed in back-quotes (` . . . `).
Legal literal identifiers:
- `x` `<clinit>` `yield`
- using back-quotes, you can more or less give any name to a field identifier.
val ` ` = 0
Back-quotes are necessary when Java identifiers clash with Scala reserved words:
Thread.`yield`()
11. LITERAL IDENTIFIERS
- Case statements. The convention is that Lower case names refer to match variables; Upper case names refer to identifiers from the outer
scope.
val A = "a"
val b = "b"
"a" match {
case b => println("b")
case A => println("A")
} //case A were unreachable
"a" match {
case `b` => println("b")
case A => println("A")
} // prints "A".
12. Scala Keywords:
abstract case catch class
def do else extends
false final finally for
forSome if implicit import
lazy match new null
object override package private
protected return sealed super
this throw trait try
true type val var
while with yield
- : = =>
<- <: <% >:
# @
13. Comments in Scala
object HelloWorld {
/* This is my first Scala program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
* /* Nested comments are available */
*/
def main(args: Array[String]) {
// Prints Hello World
// This is also an example of single line comment.
println("Hello, world!")
}
}
14. Newline Characters
Scala is a line-oriented language: statements may be terminated by
semicolons (;) or newlines.
def swap(i: Int, j: Int) {
val t = xs(i)
xs(i) = xs(j)
xs(j) = t()
}
Semicolon is required if you write multiple statements on a single line:
val s = "hello"; println(s)
15. Scala mode and XML mode
● Start XML scanning when “<” is encountered
● Finish XML scanning after successful parse
● Scala expressions are embeddable in XML
● Need to keep stack of Scala/XML nesting
● No Scala tokens in XML mode
16. Scala mode and XML mode
var xmlBook =
<book>
<title>Scala</title>
<version>{book.ver}</version>
</book>
17. References:
- The Scala Language Specification Version 2.9
Martin Odersky, 11 June 2014
- Scala for the impatient
Cay S. Hostman
Case Sensitivity - Scala is case-sensitive, which means identifier Hello and hellowould have different meaning in Scala.
Class Names - For all class names, the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
Example class MyFirstScalaClass
Method Names - All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.
Example def myMethodName()
Program File Name - Name of the program file should exactly match the object name. When saving the file you should save it using the object name (Remember scala is case-sensitive) and append '.scala' to the end of the name. (if the file name and the object name do not match your program will not compile).
Example: Assume 'HelloWorld' is the object name. Then the file should be saved as'HelloWorld.scala'
def main(args: Array[String]) - Scala program processing starts from the main() method which is a mandatory part of every Scala Program.
An alphanumeric identifier starts with a letter or underscore, which can be followed by further letters, digits, or underscores.
The '$' character is a reserved keyword in Scala and should not be used in identifiers. Following are legal alphanumeric identifiers:
age, salary, _value, __1_value
Following are illegal identifiers:
$salary, 123abc, -salary
An operator identifier consists of one or more operator characters. Operator characters are printable ASCII characters such as +, :, ?, ~ or #. Following are legal operator identifiers:
+ ++ ::: <?> :>
The Scala compiler will internally "mangle" operator identifiers to turn them into legal Java identifiers with embedded $ characters. For instance, the identifier :-> would be represented internally as $colon$minus$greater.
A mixed identifier consists of an alphanumeric identifier, which is followed by an underscore and an operator identifier. Following are legal mixed identifiers:
unary_+, myvar_=
Here, unary_+ used as a method name defines a unary + operator and myvar_= used as method name defines an assignment operator.
A literal identifier is an arbitrary string enclosed in back ticks (` . . . `). Following are legal literal identifiers:
`x` `<clinit>` `yield`
The literal definition of identifiers is useful in two cases. The first case is, when there is already a reserved word of the same name in Scala and you need to use a Java library which does not care about that (and of course, why should it).
using backticks, you can more or less give any name to a field identifier.
val ` ` = 0
which defines a variable with name (one character of whitespace).
The other use case comes with case statements. The convention is that lower case names refer to match variables, whereas upper case names refer to identifiers from the outer scope. So,
val A = "a" val b = "b" "a" match { case b => println("b") case A => println("A") }
prints "b" (if the compiler were dumb enough not to fail with saying case A were unreachable). If you want to refer to the originally defined val b, you need to use backticks as a marker.
"a" match { case `b` => println("b") case A => println("A") }
Which prints "A".
Scala supports single-line and multi-line comments very similar to Java. Multi-line comments may be nested, but are required to be properly nested. All characters available inside any comment are ignored by Scala compiler.
Scala is a line-oriented language where statements may be terminated by semicolons (;) or newlines. A semicolon at the end of a statement is usually optional. You can type one if you want but you don't have to if the statement appears by itself on a single line. On the other hand, a semicolon is required if you write multiple statements on a single line:
val s = "hello"; println(s)