Basic Scala

advertisement
Writing Scala Programs
Command Line

There are three common operating systems:






Windows (various flavors; I recommend Windows 7)
UNIX or Linux (basically the same thing)
Mac OS X, built on top of UNIX
All have a “command line” or “shell” interface
Mac: Use the Terminal application
Windows: Two choices. From the Start menu, type:


cmd, to get the DOS interface (not recommended)
powershell, to get a better interface



PowerShell has many conveniences, including some UNIX-like commands
You can copy the PowerShell icon to your Start menu for easier access
Once everything has been installed properly, you can type
scala at the command line to enter the Scala REPL
2
Minor points

Little things add up!


Syntax coloring is when your editor uses different colors for keywords, strings,
numbers, etc.




Parenthesis matching
Automatic indentation
Indent/dedent large blocks of code
Use a good font, so that you don’t even have to think about distinguishing a 1 (one)
from an l (lowercase L) or a | (vertical bar)


You can ignore the colors; you’ll soon start to notice when they indicate errors
Other editor features you should learn to use:


“Watch the pennies and the dollars will take care of themselves.”—Benjamin Franklin
My favorite is Consolas (compare 1 and l, for example)
Not so minor: RSI, Repetitive Strain Injury, is an occupational hazard for
programmers


Learn at least the very basics of ergonomics
www.codinghorror.com/blog/2007/08/computer-workstation-ergonomics.html
3
Editors


You should get a good editor and get familiar with its features
The following work on both Mac and Windows, and have syntax
coloring for Scala

Sublime Text 2 or newer—recommended by Atomic Scala






Excellent, but it will nag you occasionally to pay for it
Oddly, it has no way to access a printer (to make paper copies)
jEdit is a longtime favorite of mine
For the Mac, TextMate is a free alternative to Sublime Text 2
For Windows, Notepad++ is a fine editor but does not come with
Scala syntax coloring
If you are already familiar with Eclipse, there is a Scala plugin
for it
4
The REPL


REPL stands for “Read-Eval-Print-Loop”
You can enter expressions directly in the REPL, and the result will be
printed (That’s the “P” in REPL)



You can also use print and println in the REPL, but usually you don’t
need to





scala> "Hello from the REPL!"
res1: String = Hello from the REPL!
This tells you that the REPL has put your result in a val named res1 that
you can use later, and that res1 is a String
scala> println("Hello from the REPL!")
Hello from the REPL!
The result of calling println is “unit”
Unit is , and the REPL usually doesn’t print it
You can give commands to the REPL, such as :help, :load, and :quit
To load a program from a file into the REPL, use :load

For example, to load the program Hello.scala, enter :load
Hello.scala
5
More about the REPL

Because the REPL is designed for trying things out, it lets you redeclare a val:

scala> val x = 1
x: Int = 1
scala> x = 2
<console>:8: error: reassignment to val
x = 2
^


scala> val x = 2
x: Int = 2
You can’t do this in a “real” program
In the REPL, if an expression seems to be complete, it will be executed


if (x < y) min = x // will execute as soon as you hit Enter
else min = y
// then this will be an error
You can avoid this problem by using :paste (see :help)
6
Programs

A program must contain a main method inside an object:




object HelloWorld {
def main(args: Array[String]) {
println("Hello, World!")
}
}
Running the program from the (PowerShell) command line:
PS C:\Users\dave\Scala programs> scala HelloWorld.scala
Hello, World!
Loading and running the program from the Scala REPL:
scala> :load HelloWorld.scala
Loading HelloWorld.scala...
defined module HelloWorld
scala> HelloWorld.main(Array())
Hello, World!
Note: Array() is a required argument to the method
7
Scripts



A script is a file containing any number of Scala expressions to be executed

On the file:
val hello = "Hello, Scala!"
println(hello + " (scripted)")

Running the script from the (PowerShell) command line:
PS C:\Users\dave\Scala programs> scala ScriptDemo.scala
Hello, Scala! (scripted)

Running the script from the REPL:
scala> :load ScriptDemo.scala
Loading ScriptDemo.scala...
Hello, Scala! (scripted)
How this works: Scala turns your script into a program by creating an object
containing a main method, and putting your script inside that main method
Scripts are useful for small, simple programs that you might want to run from
the command line
8
The End
9
Download