Hello World (Scala)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

This example shows the well-known "Hello World!" example implemented in Scala.

You define it through the keyword object which introduces a Singleton object

<<HelloWorld.scala>>=
object HelloWorld {
  def main(args: Array[String]) = {
    Console.println("Hello, world!");
  }
}

You can also use the Application mixin that saves you from defining the main method for simple examples and executes from the singleton body

<<HelloWorld2.scala>>=
object HelloWorld extends Application {
   Console.println("Hello, world!");
}

Compiling and running

If you have installed Scala, the HelloWorld.scala program can be compiled using the scala compiler. To compile to an executable, the compiler should be invoked as follows:

scalac HelloWorld.scala

The resulting helloworld executable can be run from the command line.

scala HelloWorld
Download code
Views
Personal tools