sign in
Home | Updates | Pages | Users | Admin | Help

scalastudy

Scala Study

Why?

Scala programs compile to JVM bytecodes. Scala code can also be invoked from Java code. Scala programs tend to be short. Scala programmers have reported reductions in number of lines of up to a factor of ten compared to Java.

First Steps

To enter something into the interpreter that spans multiple lines, just keep typing after the first line. If the code you typed so far is not complete, the interpreter will respond with a vertical bar on the next line. If you realize you have typed something wrong, but the interpreter is still waiting for more input, you can escape by pressing enter twice.

Scala has two kinds of variables, vals and vars. vals are similar to final variables in Java. Once initialized, a val can never be reassigned. vars, by contrast, are similar to non-final variables in Java. A var can be reassigned throughout its lifetime.

A result type of Unit indicates the function returns no usable value. Scala’s Unit type is similar to Java’s void type, and in fact every void-returning method in Java is mapped to a Unit-returning method in Scala.

The syntax of an anonymous function in Scala

The syntax for an anonymous function is a list of named parameters, in parentheses, a right arrow, and then the body of the function.

(x: Int, y: Int) => x + y

// example:
args.foreach((arg: String) => println(arg))
If an anonymous function consists of one statement that takes a single argument, you need not explicitly name and specify the argument. Thus, the following code also works:
args.foreach(println)
In an effort to guide you in a functional direction, only a functional relative of the imperative for (called a for expression) is available in Scala:
for (arg <- args)
  println(arg)
For each element of the args array, a new arg val will be created and initialized to the element value, and the body of the for will be executed.

Next steps

If code contains any vars, variables that can be reassigned, it is probably in the imperative style. If the code contains no vars at all — i.e., it contains only vals — it is probably in the functional style. Thus one way to move towards a functional style is to try to program without any vars.

Parameterize Arrays with types

val s = new String("Hello, world!")
println(s)
In the previous example, you parameterize the String instance with the initial value “Hello, world!”.

Parameterization means configuring an instance at the point in your program that you create that instance.

In addition to parameterizing instances with values at the point of instantiation, you can also parameterize them with types:
val greetStrings = new Array[String](3)

greetStrings(0) = "Hello" 
greetStrings(1) = ", " 
greetStrings(2) = "world!\n" 

for (i <- 0 to 2)
  print(greetStrings(i))

!!! When you define a variable with val, the variable can’t be reassigned, but the object to which it refers could potentially still be mutated.


Powered by JunebugWiki v0.0.31 Page last edited by stoyan on December 21, 2007 11:04 AM (diff)
[readonly] Version 2 (current) «olderversions