Scala (programming language)
From Wikipedia, the free encyclopedia
Paradigm | Multi-paradigm: functional, object-oriented |
---|---|
Appeared in | 2003 |
Designed by | Martin Odersky |
Developer | Programming Methods Laboratory of EPFL |
Latest release | 2.7.3/ January 13, 2009 |
Typing discipline | static, strong, inferred |
Influenced by | Java, Pizza,[1] Haskell, Erlang, Standard ML, Objective Caml, Smalltalk |
Website | www.scala-lang.org |
Scala (pronounced /ˈskala/) is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming.[1]
Contents |
[edit] Platform
Scala runs on the Java platform (Java Virtual Machine) and is compatible with existing Java programs. It also runs on Java Platform, Micro Edition Connected Limited Device Configuration.[2] An alternative implementation exists for the .NET platform, but it has not been kept up to date.[citation needed] The Scala software distribution, including compiler and libraries, is released under a BSD license.[3]
[edit] History
The design of Scala started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) by Martin Odersky, following on from work on Funnel, a programming language combining ideas from functional programming and Petri nets.[4] Odersky had previously worked on Generic Java and javac, Sun's Java compiler.[4] Scala was released late 2003 / early 2004 on the Java platform, and on the .NET platform in June 2004.[1][4][5] A second version of the language, v2.0, was released in March 2006.[1] As of January 2009, the latest release is version 2.7.3. In April 2009 Twitter announced they had switched large portions of their backend to Scala and intended to covert the rest.[6]
[edit] Object-oriented features
Scala is a pure object-oriented language in the sense that every value is an object. Data types and behaviors of objects are described by classes and traits. Class abstractions are extended by subclassing and by a flexible mixin-based composition mechanism to avoid the problems of multiple inheritance.
[edit] Functional programming
Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, supports higher-order functions, allows functions to be nested, and supports currying. Scala's case classes and its built-in support for pattern matching model algebraic types used in many functional programming languages.
Furthermore, Scala's notion of pattern matching naturally extends to the processing of XML data with the help of regular expression patterns. In this context, sequence comprehensions are useful for formulating queries.
Implementing some of the most advanced functional programming constructs in Scala may occasionally be limited by the fact that tail call optimizations are not supported completely, because the JVM lacks the byte codes for implementing them efficiently. In these situations one simply has to go back to traditional procedural programming.
An implementation of a Quicksort algorithm in functional style, for comparison with the Erlang Quicksort example:
def qsort(lst: List[Int]):List[Int] = lst match { case Nil => Nil case pivot::tail => qsort(tail filter { _ < pivot }) ::: pivot :: qsort(tail filter { _ >= pivot }) }
[edit] Static typing
Scala is equipped with an expressive type system that enforces statically that abstractions are used in a safe and coherent manner. In particular, the type system supports:
- generic classes,
- variance annotations,
- upper and lower type bounds,
- classes and abstract types as object members,
- compound types,
- explicitly typed self references,
- views, and
- polymorphic methods.
[edit] 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 unique 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 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.
[edit] Platform independence
Scala is designed to interoperate well with popular programming environments like the Java 2 Runtime Environment (JRE) and the .NET CLR.[2]. Scala can make use of all libraries available for Java/C#. Scala has the same compilation model (separate compilation, dynamic class loading) as Java and C#, and allows thousands of high-quality libraries to be accessed.
[edit] Frameworks using Scala
Lift is a free web application framework. It aims to deliver similar benefits to Ruby on Rails, except that Lift applications are written in Scala instead of Ruby. The use of Scala means that any existing Java library and Web container can be used in running Lift applications.
[edit] "Hello world" example
Here is the canonical Hello world program written in Scala:
object HelloWorld extends Application { println("Hello, world!") }
or
object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }
Notice how similar it is to the stand-alone Hello World application for Java. The notable difference is that nothing is declared to be static or void; the singleton created by the object keyword removes the need to invoke any such construct.
Assuming the program is saved in a file named HelloWorld.scala, it can then be compiled from the command line:
> scalac HelloWorld.scala
To run it:
> scala -classpath . HelloWorld
This is analogous to how a Java "hello world" program is compiled and run. Indeed, Scala's compilation and execution model is identical to that of Java, making it compatible with Java build tools such as Ant.
It is also possible to feed this program directly into the Scala interpreter, using the option -i (to load code from the file) and the option -e (to execute additional code needed to actually invoke the HelloWorld object's method):
> scala -i HelloWorld.scala -e 'HelloWorld.main(null)'
[edit] Testing
There are some ways to test code in Scala:
- ScalaTest
- The Rehersal project provides a more extensive framework using Expectations and natural language for test names. It also has integration with Apache Ant.
- ScalaCheck, a library similar to Haskell's QuickCheck
- specs, a Behavior driven development library for Scala
- JUnit
The built-in Scala library SUnit is deprecated as of version 2.8.0, see SUnit documentation.
[edit] References
- ^ a b c d Martin Odersky et al, An Overview of the Scala Programming Language, 2nd Edition
- ^ a b "Scala on .NET". Programming Methods Laboratory of EPFL. 2008-01-07. http://www.scala-lang.org/docu/clr/. Retrieved on 2008-01-15. "Scala is primarily developed for the JVM and embodies some of its features. Nevertheless, its .NET support is designed to make it as portable across the two platforms as possible."
- ^ http://www.scala-lang.org/node/146
- ^ a b c Martin Odersky, "A Brief History of Scala", Artima.com weblogs, June 9, 2006
- ^ Martin Odersky, "The Scala Language Specification Version 2.7"
- ^ Greene, Kate (April 1, 2009). "The Secret Behind Twitter's Growth, How a new Web programming language is helping the company handle its increasing popularity.". Technology Review. MIT. http://www.technologyreview.com/blog/editors/23282/?nlid=1908. Retrieved on April 6, 2009.
[edit] Books
- Programming in Scala - A comprehensive step-by-step guide by Martin Odersky, Lex Spoon, and Bill Venners.
[edit] External links
- Scala website
- Scala Wiki
- Literate Programs - Scala
- The Scala Search Engine
- Lift Web Framework
- Podcast Interview with Martin Odersky on Scala
- Scala Presentation From The Googleplex [1] (video) - given by Martin Odersky, creator of the language; accompanying slides from the talk
- Scalaz
- Scala community blogs