Scala programming language
Encyclopedia
Scala is a multi-paradigm
Multi-paradigm programming language
Programming languages can be grouped by the number and types of paradigms supported.-Paradigm summaries:A concise reference for the programming paradigms listed in this article....

 programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 designed to integrate features of object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 and functional programming
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

. The name Scala is a portmanteau of "scalable" and "language", signifying that it is designed to grow with the demands of its users. James Strachan
James Strachan (programmer)
James Strachan is a programmer who created the Groovy in 2003. He is a member of the Apache Software Foundation and a cofounder of a number of other open source projects such* Apache ActiveMQ...

, the creator of Groovy, described Scala as a possible successor to Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

.

History

The design of Scala started in 2001 at the École Polytechnique Fédérale de Lausanne
École polytechnique fédérale de Lausanne
The École polytechnique fédérale de Lausanne is one of the two Swiss Federal Institutes of Technology and is located in Lausanne, Switzerland.The school was founded by the Swiss Federal Government with the stated mission to:...

 (EPFL) by Martin Odersky
Martin Odersky
Martin Odersky is a German computer scientist and professor of programming methods at the EPFL. He specialises in code analysis and programming languages.In 1989 Odersky received his Ph.D...

, following on from work on Funnel, a programming language combining ideas from functional programming and Petri net
Petri net
A Petri net is one of several mathematical modeling languages for the description of distributed systems. A Petri net is a directed bipartite graph, in which the nodes represent transitions and places...

s. Odersky had previously worked on Generic Java and javac
Javac
javac is the primary Java compiler, included in the Java Development Kit from Oracle Corporation.The compiler accepts source code conforming to the Java language specification and produces bytecode conforming to the Java Virtual Machine Specification .javac is itself written in Java...

, Sun's Java compiler.

Scala was released late 2003 / early 2004 on the Java platform, and on the .NET platform in June 2004. A second version of the language, v2.0, was released in March 2006.

On 17 January 2011 the Scala team won a 5 year research grant of over €2.3 million from the European Research Council
European Research Council
The European Research Council is the independent body that funds investigator-driven frontier research in the European Union . It is part of the Seventh Research Framework Programme ....

. On 12 May 2011, Odersky and collaborators launched Typesafe, a company to provide commercial support, training, and services for Scala. Typesafe received $3 million investment from Greylock Partners.

Platforms and license

Scala runs on the Java platform (Java Virtual Machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

) and is compatible with existing Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 programs. It also runs on Android smartphones. An alternative implementation exists for the .NET platform.

Scala has the same compilation model as Java and C# (separate compilation, dynamic class loading
Java Classloader
The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of class loaders...

), so Scala code can call Java libraries (or .NET libraries in the .NET implementation).

Scala's operational characteristics are the same as Java's. The Scala compiler generates byte code that is nearly identical to that generated by the Java compiler. In fact, Scala code can be decompiled
Decompiler
A decompiler is the name given to a computer program that performs, as far as possible, the reverse operation to that of a compiler. That is, it translates a file containing information at a relatively low level of abstraction into a form having a higher level of abstraction...

 to readable Java code, with the exception of certain constructor operations. To the JVM, Scala code and Java code are indistinguishable. The only difference is a single extra runtime library, scala-library.jar.

The Scala software distribution, including compiler and libraries, is released under a BSD license.

Software using Scala

Lift
Lift (web framework)
Lift is a free web application framework that is designed for the Scala programming language. It was originally created by who was dissatisfied with certain aspects of the Ruby on Rails framework. Lift was launched as an open-source project on February 26, 2007 under the Apache 2.0 license...

 is a free
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...

 web application framework
Web application framework
A web application framework is a software framework that is designed to support the development of dynamic websites, web applications and web services. The framework aims to alleviate the overhead associated with common activities performed in Web development...

 that aims to deliver benefits similar to Ruby on Rails
Ruby on Rails
Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language.-History:...

. The use of Scala means that any existing Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 library and Web container
Web container
Web container is the component of a web server that interacts with the servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights...

 can be used in running Lift applications.

In April 2009 Twitter
Twitter
Twitter is an online social networking and microblogging service that enables its users to send and read text-based posts of up to 140 characters, informally known as "tweets".Twitter was created in March 2006 by Jack Dorsey and launched that July...

 announced they had switched large portions of their backend from Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

 to Scala and intended to convert the rest. In addition, Foursquare
Foursquare (service)
Foursquare, stylized as foursquare, is a location-based social networking website for mobile devices, such as smartphones. Users "check-in" at venues using a mobile website, text messaging or a device-specific application by selecting from a list of venues the application locates nearby...

 uses Scala and Lift.

GridGain provides Scala-based DSL for cloud computing.

In April 2011, The Guardian
The Guardian
The Guardian, formerly known as The Manchester Guardian , is a British national daily newspaper in the Berliner format...

newspaper's website guardian.co.uk
Guardian.co.uk
guardian.co.uk, formerly known as Guardian Unlimited, is a British website owned by the Guardian Media Group. Georgina Henry is the editor...

 announced that it was switching from Java to Scala, starting with the Content API for selecting and collecting news content. The website is one of the highest-traffic English-language news websites, and according to its editor has the second largest online readership of any English-language newspaper in the world, after the New York Times.

Swiss bank UBS approved Scala for general production usage.

Functional programming

Scala supports functional programming
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

. The language provides a lightweight syntax for defining anonymous function
Anonymous function
In programming language theory, an anonymous function is a function defined, and possibly called, without being bound to an identifier. Anonymous functions are convenient to pass as an argument to a higher-order function and are ubiquitous in languages with first-class functions such as Haskell...

s, supports higher-order function
Higher-order function
In mathematics and computer science, higher-order functions, functional forms, or functionals are functions which do at least one of the following:*take one or more functions as an input*output a function...

s, allows functions to be nested, and supports currying
Currying
In mathematics and computer science, currying is the technique of transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions each with a single argument...

. Using the keyword lazy defers the initialization of a value until this value is used. Delimited continuation
Delimited continuation
In programming languages, a delimited continuation, composable continuation or partial continuation, is a "slice" of a continuation frame that has been reified into a function...

s are supported since version 2.8.

Scala has case classes and built-in support for pattern matching
Pattern matching
In computer science, pattern matching is the act of checking some sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact. The patterns generally have the form of either sequences or tree structures...

. These features can be used to model the algebraic data type
Algebraic data type
In computer programming, particularly functional programming and type theory, an algebraic data type is a datatype each of whose values is data from other datatypes wrapped in one of the constructors of the datatype. Any wrapped datum is an argument to the constructor...

s used in many functional programming languages.

Tail call
Tail call
In computer science, a tail call is a subroutine call that happens inside another procedure and that produces a return value, which is then immediately returned by the calling procedure. The call site is then said to be in tail position, i.e. at the end of the calling procedure. If a subroutine...

 optimization is not supported completely, because the JVM lacks tail call support. In simple cases, the Scala compiler can optimize tail calls into loops.

An implementation of a sorting algorithm (similar to quicksort) in functional style:


def qsort: List[Int] => List[Int] = {
case Nil => Nil
case pivot :: tail =>
val (smaller, rest) = tail.partition(_ < pivot)
qsort(smaller) ::: pivot :: qsort(rest)
}

Object-orientation

Scala is a pure object-oriented language in the sense that every value is an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

. Data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

s and behaviors of objects are described by classes
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

 and traits. Class abstractions are extended by subclassing and by a flexible mixin
Mixin
In object-oriented programming languages, a mixin is a class that provides a certain functionality to be inherited or just reused by a subclass, while not meant for instantiation , Mixins are synonymous functionally with abstract base classes...

-based composition mechanism to avoid the problems of multiple inheritance
Multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass....

.

Static typing

Scala is equipped with an expressive static type system that enforces the safe and coherent use of abstractions. In particular, the type system supports:
  • Classes and abstract type
    Abstract type
    In programming languages, an abstract type is a type in a nominative type system which cannot be instantiated. An abstract type may have no implementation, or an incomplete implementation...

    s as object members
  • Compound types
  • Explicitly typed self references
  • Generic classes
    Generic programming
    In a broad definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters...

  • Polymorphic methods
  • Upper and lower type bounds
  • Variance
  • Annotation
    Annotation
    An annotation is a note that is made while reading any form of text. This may be as simple as underlining or highlighting passages.Annotated bibliographies give descriptions about how each source is useful to an author in constructing a paper or argument...

  • Views


Scala is able to infer types by usage. This makes most static type declarations optional. Static types need not be explicitly declared unless a compiler error indicates the need. In practice, some static type declarations are included for the sake of code clarity. Lack of explicit type declarations gives Scala the appearance of a dynamically typed language.

Extensibility

The design of Scala acknowledges the fact that, in practice, the development of domain-specific applications often requires domain-specific language extensions. Scala provides a novel combination of language mechanisms that make it easy to smoothly add new language constructs in the form of libraries:
  • any method may be used as an infix or postfix operator, and
  • closures
    Closure (computer science)
    In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...

     are constructed automatically depending on the expected type (target typing).


A joint use of both features facilitates the definition of new statements without extending the syntax and without using macro-like meta-programming facilities.

Concurrency

Scala has in its standard library support for the actor model
Actor model
In computer science, the Actor model is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and...

, in addition to the standard Java concurrency APIs. An alternative CSP
Communicating sequential processes
In computer science, Communicating Sequential Processes is a formal language for describing patterns of interaction in concurrent systems. It is a member of the family of mathematical theories of concurrency known as process algebras, or process calculi...

 implementation for channel-based message passing is Communicating Scala Objects.

"Hello world" example

Here is the classic Hello world program
Hello world program
A "Hello world" program is a computer program that outputs "Hello world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language, or to...

 written in Scala:


object HelloWorld extends App {
println("Hello, world!")
}


Unlike the stand-alone Hello World application for Java, there is no class declaration and nothing is declared to be static; a singleton object
Singleton pattern
In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system...

 created with the object keyword is used instead.

With the program saved in a file named HelloWorld.scala, it can be compiled from the command line:

$ scalac HelloWorld.scala

To run it:

$ scala -classpath . HelloWorld

This is analogous to the process for compiling and running Java code. Indeed, Scala's compilation and execution model is identical to that of Java, making it compatible with Java build tools such as Ant
Apache Ant
Apache Ant is a software tool for automating software build processes. It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects....

.

A shorter version of the "Hello world" Scala program is:


println("Hello, world!")

Saved in a file named HelloWorld2.scala, this can be run as a script without prior compilation using:

$ scala HelloWorld2.scala

Commands can also be fed directly into the Scala interpreter, using the option -e, and escaping the quotes using \:

$ scala -e "println(\"Hello, World!\")"

Testing

There are several ways to test code in Scala:
  • ScalaTest supports multiple testing styles and can integrate with Java-based testing frameworks
  • ScalaCheck, a library similar to Haskell's QuickCheck
    QuickCheck
    QuickCheck is a combinator library written in Haskell, designed to assist in software testing by generating test cases for test suites. It is compatible with the GHC compiler and the Hugs interpreter....

  • specs2, a library for writing executable software specifications
  • ScalaMock provides support for testing high-order and curried functions
  • JUnit
    JUnit
    JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks collectively known as xUnit that originated with SUnit....

     or TestNG
    TestNG
    - Features :TestNG features include:* Flexible test configuration.* Support for data-driven testing .* Support for multiple instances of the same test class * Support for parameters....

    , two popular testing frameworks written in Java

See also

  • Circumflex, Web application and other frameworks for Scala
  • Lift
    Lift (web framework)
    Lift is a free web application framework that is designed for the Scala programming language. It was originally created by who was dissatisfied with certain aspects of the Ruby on Rails framework. Lift was launched as an open-source project on February 26, 2007 under the Apache 2.0 license...

    , a Web application framework for Scala
  • Play!
    Play Framework
    Play is an open source web application framework, written in Java, which follows the model-view-controller architectural pattern. It aims to optimize developer productivity by using convention over configuration, hot code reloading and display of errors in the browser.Although Play is written in...

    , a Web application framework which supports Scala
  • ZK Framework
    ZK (framework)
    ZK is an open-source Ajax Web application framework, written in Java, that enables creation of rich graphical user interfaces for Web applications with no JavaScript and little programming knowledge....

    , a Web application framework which supports many languages including Java, Groovy, Ruby and Scala
  • Scala IDE, open source Scala IDE for Eclipse

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK