Scala (programming language)
Paradigm | Multi-paradigm: functional, object-oriented, imperative |
---|---|
Designed by | Martin Odersky |
Developer | Programming Methods Laboratory of EPFL |
First appeared | 2003 |
Stable release | 2.8.1
/ November 9, 2010 |
Typing discipline | static, strong, inferred, structural |
Platform | JVM |
License | BSD |
Website | www.scala-lang.org |
Influenced by | |
Java, Pizza,[1] Haskell, Erlang, Standard ML, Objective Caml, Smalltalk, Scheme | |
Influenced | |
Fantom | |
|
Scala (Template:Pron-en SKAH-lə) is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming.[1] The name Scala stands for "scalable language", signifying that it is designed to grow with the demands of its users.
Platforms and license
Scala runs on the Java platform (Java Virtual Machine) and is compatible with existing Java programs. It also runs on Android smartphones. An alternative implementation exists for the .NET platform,[2] but it has not been kept up to date.[3]
Scala has the same compilation model as Java and C# (separate compilation, dynamic class loading), 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 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.[4]
The Scala software distribution, including compiler and libraries, is released under a BSD license.[5]
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.[6]
This article needs additional citations for verification. (January 2011) |
Odersky had previously worked on Generic Java and javac, Sun's Java compiler.[6] Scala was released late 2003 / early 2004 on the Java platform, and on the .NET platform in June 2004.[1][6][7] A second version of the language, v2.0, was released in March 2006.[1]
On 17 January 2011 Scala would be provided some funding by the European Research Council.
As of November 2010[update], the latest release is version 2.8.1.
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.
Functional programming
Scala also supports functional programming. The language provides a lightweight syntax for defining anonymous functions, supports higher-order functions, allows functions to be nested, and supports currying. Using the keyword lazy defers the initialization of a value until this value is used. Evaluation of delimited continuations is supported in version 2.8.
Scala's case classes and its built-in support for pattern matching model Algebraic data types used in many functional programming languages.
Tail call optimization is not supported completely, because the JVM lacks tail call support. In simple cases, the Scala compiler can optimize tail calls into loops.[8]
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)
}
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:
- 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.
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 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 built-in support for the actor model, in addition to the standard Java concurrency APIs. An alternative CSP implementation for channel-based message passing is Communicating Scala Objects.[9]
Software using Scala
Lift is a free web application framework that aims to deliver benefits similar to Ruby on Rails. The use of Scala means that any existing Java library and Web container can be used in running Lift applications.
In April 2009 Twitter announced they had switched large portions of their backend from Ruby to Scala and intended to convert the rest.[10] In addition Foursquare uses Scala and Lift.[11]
GridGain provides Scala-based DSL for cloud computing.[12]
"Hello world" example
Here is the classic 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. A notable difference is that nothing is declared to be static; the singleton created by the object keyword is used instead.
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)'
An alternative shorter version of the Hello World scala program is
println("Hello, world!")
which if saved in a file called HelloWorld2.scala, can then simply be run using:
> scala HelloWorld2.scala
Testing
There are some ways to test code in Scala:
- ScalaTest
- ScalaCheck, a library similar to Haskell's QuickCheck
- specs, a Behavior driven development library for Scala
- specs2, a rewrite and extension of the previous library
- JUnit
The built-in Scala library SUnit was removed in version 2.8.0.
See also
- Lift, a web application framework for Scala
- Play!, a web application framework which supports Scala
- Circumflex, web application and other frameworks for Scala
References
- ^ a b c d Martin Odersky et al., An Overview of the Scala Programming Language, 2nd Edition
- ^ "Scala on .NET". Programming Methods Laboratory of EPFL. 2008-01-07. Archived from the original on 2007-10-09. Retrieved 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.artima.com/weblogs/viewpost.jsp?thread=163733
- ^ http://blog.lostlake.org/index.php?/archives/73-For-all-you-know,-its-just-another-Java-library.html
- ^ 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"
- ^ Tail calls, @tailrec and trampolines
- ^ Communicating Scala Objects, Bernard Sufrin, Communicating Process Architectures 2008
- ^ 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. Retrieved April 6, 2009.
- ^ Scala, Lift, and the Future
- ^ Introducing Scalar - Scala-based DSL for Cloud Computing
Further reading
- Suereth, Joshua D. (Spring, 2011), Scala in Depth, Manning Publications, p. 225, ISBN 9781935182702
{{citation}}
: Check date values in:|date=
(help) - Meredith, Gregory (?, 2011), Monadic Design Patterns for the Web (PDF) (1st ed.), ?, p. 300, ISBN X
{{citation}}
: Check|isbn=
value: length (help); Check date values in:|date=
(help) - Raychaudhuri, Nilanjan (Spring 2010), Scala in Action (1st ed.), [Manning, p. 525, ISBN 9781935182757
- Wampler, Dean; Payne, Alex (September 15, 2009), Programming Scala: Scalability = Functional Programming + Objects (1st ed.), O'Reilly Media, p. 448, ISBN 0596155956
- Odersky, Martin; Spoon, Lex; Venners, Bill (November 26, 2008), Programming in Scala: A Comprehensive Step-by-step Guide (1st ed.), Artima Inc, p. 776, ISBN 0981531601
- Pollak, David (May 25, 2009), Beginning Scala (1st ed.), Apress, p. 776, ISBN 1430219890
- Perrett, Tim (April 2010), Lift in Action (1st ed.), Manning, p. 450, ISBN 9781935182801
- Loverdos, Christos; Syropoulos, Apostolos (September 2010), Steps in Scala: An Introduction to Object-Functional Programming (1st ed.), Cambridge University Press, pp. xviii + 485, ISBN 9780521747585
- Subramaniam, Venkat (July 28, 2009), Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine (1st ed.), Pragmatic Bookshelf, p. 250, ISBN 193435631X
External links
- Scala website
- Simply Scala online version of the Scala REPL - allows you to try out Scala using a web browser
- Scala Wiki
- Literate Programs - Scala
- The Scala Search Engine
- Lift Web Framework
- O/R Broker
- Podcast Interview with Martin Odersky on Scala
- Scala Presentation From The Googleplex (video) - given by Martin Odersky, creator of the language; accompanying slides from the talk
- Scalaz
- Scala community blogs
- Lift Slides from David Pollak's lift video, contains a presentation of Scala
- Scala articles directory
- Scala for Java Refugees
- Scala Forum