Tuesday, April 5, 2011

Tech Talk - Intro scala

Scala fuses object-oriented and functional programming in a statically typed programming language. It is aimed at the construction of components and component system.It smoothly integrates object-oriented and functional programming.

It is designed to express common programming patterns in a concise, elegant, and type-safe way. Scala introduces several innovative language constructs. For instance:
    • Abstract types and mixin composition unify concepts from object and module
    • Pattern matching over class hierarchies unifies functional and object-
      oriented data access. It greatly simplifies the processing of XML trees.
    • A flexible syntax and type system enables the construction of advanced li-
      braries and new domain specific languages.

At the same time, Scala is compatible with Java. Java libraries and frameworks can be used without glue code or additional declarations.

Simple Example

As a first example, we will use the standard Hello world program.It is not very fascinating but makes it easy to demonstrate the use of the Scala tools without knowing too much about the language. Here is how it looks:

object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")

The structure of this program should be familiar to Java programmers: it consists of one method called main which takes the command line arguments, an array of strings, as parameter; the body of this method consists of a single call to the predefined method println with the friendly greeting as argument. The main method does not return a value (it is a procedure method). Therefore, it is not necessary to declare a return type.

What is less familiar to Java programmers is the object declaration containing the main method. Such a declaration introduces what is commonly known as a singleton object, that is a class with a single instance. The declaration above thus declares both a class called HelloWorld and an instance of that class, also called HelloWorld.

This instance is created on demand, the first time it is used.
The astute reader might have noticed that the main method is not declared as static here. This is because static members (methods or fields) do not exist in Scala. Rather than defining static members, the Scala programmer declares these members in singleton objects.

Compiling this example

To compile the example, we use scalac, the Scala compiler. scalac works like most compilers: it takes a source file as argument, maybe some options, and produces one or several object files. The object files it produces are standard Java class files.

If we save the above program in a file called HelloWorld.scala, we can compile it by issuing the following command (the greater-than sign ‘>’ represents the shell prompt and should not be typed):

> scalac HelloWorld.scala

This will generate a few class files in the current directory. One of them will be called HelloWorld.class, and contains a class which can be directly executed using the scala command

Running the example

Once compiled, a Scala program can be run using the scala command. Its usage is very similar to the java command used to run Java programs, and accepts the same options. The above example can be executed using the following command, which produces the expected output:

> scala -classpath . HelloWorld

Hello, world!

To get more information,tutorial,doc about Scala, have a look Scala official link.

No comments:

Post a Comment