Pular para o conteúdo

Conheça Walt Disney World

Scala (programming language)

Scala
Scala logo.png
Paradigm(s) Multi-paradigm: functional, object-oriented, imperative
Appeared in 2003
Designed by Martin Odersky
Developer Programming Methods Laboratory of EPFL
Stable release 2.9.2 (April 14, 2012; 23 days ago (2012-04-14)[1])
Preview release 2.10.0 M3 (April 29, 2012; 8 days ago (2012-04-29)[2])
Typing discipline static, strong, inferred, structural
Influenced by Erlang, Haskell,[3] Java, Pizza,[4] Standard ML, OCaml, Scheme, Smalltalk
Influenced Fantom, Ceylon
Platform JVM, CLR
License BSD
Usual filename extensions .scala
Website www.scala-lang.org
Wikibooks logo Scala at Wikibooks

Scala (play /ˈskɑːlə/ SKAH-lə) is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming.[4] 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, the creator of Groovy, described Scala as a possible successor to Java.[5]

Contents

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][not in citation given] 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.[4][6][7] A second version of the language, v2.0, was released in March 2006.[4]

On 17 January 2011 the Scala team won a 5 year research grant of over €2.3 million from the European Research Council.[8] 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.[9][10][11][12]

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.[13] An alternative implementation exists for the .NET platform.[14][15]

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.[16]

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

Supported paradigms

Functional programming

Scala 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. Delimited continuations have been supported since version 2.8.

Scala has case classes and built-in support for pattern matching. These features can be used to model the 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.[18]

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. 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.

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:

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 in its standard library 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.[19]

"Hello world" example

Here is the classic Hello world program 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 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.

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
  • specs2, a library for writing executable software specifications
  • ScalaMock provides support for testing high-order and curried functions
  • JUnit or TestNG, two popular testing frameworks written in Java

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.[20]

Foursquare uses Scala and Lift.[21]

GridGain provides Scala-based DSL for cloud computing.[22]

In April 2011, The Guardian newspaper's website guardian.co.uk announced that it was switching from Java to Scala,[23] starting with the Content API for selecting and collecting news content.[24] 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.[25]

Swiss bank UBS approved Scala for general production usage.[26]

LinkedIn uses the Scalatra microframework to power its Signal API.[27]

See also

  • Circumflex, Web application and other frameworks for Scala
  • Lift, a Web application framework for Scala
  • Play!, a Web application framework which supports Scala
  • Scalatra, a very minimal Web application framework built using Scala

References

  1. ^ "Scala 2.9.2 final". 2012-04-14. http://www.scala-lang.org/node/12603. Retrieved 2012-04-29. 
  2. ^ "Scala 2.10.0 Milestone 3". 2012-04-30. http://www.scala-lang.org/node/12659. Retrieved 2012-05-03. 
  3. ^ Fogus, Michael (6 August 2010). "MartinOdersky take(5) toList". Send More Paramedics. http://blog.fogus.me/2010/08/06/martinodersky-take5-tolist/. Retrieved 2012-02-09. 
  4. ^ a b c d Martin Odersky et al., An Overview of the Scala Programming Language, 2nd Edition
  5. ^ "Scala as the long term replacement for Java". http://www.infoq.com/news/2009/07/scala-replace-java. 
  6. ^ a b c Martin Odersky, "A Brief History of Scala", Artima.com weblogs, June 9, 2006
  7. ^ Martin Odersky, "The Scala Language Specification Version 2.7"
  8. ^ Scala Team Wins ERC Grant
  9. ^ "Commercial Support for Scala". 2011-05-12. http://www.scala-lang.org/node/9484. Retrieved 2011-08-18. 
  10. ^ "Why We Invested in Typesafe: Modern Applications Demand Modern Tools". 2011-05-12. http://greylockvc.com/2011/05/12/why-we-invested-in-typesafe-modern-applications-demand-modern-tools. Retrieved 2011-08-18. 
  11. ^ "Open-source Scala gains commercial backing". 2011-05-12. http://news.cnet.com/8301-13846_3-20062090-62.html. Retrieved 2011-10-09. 
  12. ^ "Cloud computing pioneer Martin Odersky takes wraps off his new company Typesafe". 2011-05-12. http://www.mercurynews.com/business/ci_18048434. Retrieved 2011-08-24. 
  13. ^ Scala IDE for Eclipse: Developing for Android
  14. ^ "Scala on .NET". Programming Methods Laboratory of EPFL. 2008-01-07. Archived from the original on 2007-10-09. http://web.archive.org/web/20071009010727/http://www.scala-lang.org/docu/clr/. 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." 
  15. ^ "Scala on .Net per Miguel Garcia". 2011-07-18. http://www.scala-lang.org/node/10299. Retrieved 2011-07-30. 
  16. ^ http://blog.lostlake.org/index.php?/archives/73-For-all-you-know,-its-just-another-Java-library.html
  17. ^ http://www.scala-lang.org/node/146
  18. ^ Tail calls, @tailrec and trampolines
  19. ^ Communicating Scala Objects, Bernard Sufrin, Communicating Process Architectures 2008
  20. ^ 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 April 6, 2009. 
  21. ^ Scala, Lift, and the Future
  22. ^ Introducing Scalar - Scala-based DSL for Cloud Computing
  23. ^ "Guardian switching from Java to Scala". Heise Online. 2011-04-05. http://www.h-online.com/open/news/item/Guardian-switching-from-Java-to-Scala-1221832.html. Retrieved 2011-04-05. 
  24. ^ "Guardian.co.uk Switching from Java to Scala". InfoQ.com. 2011-04-04. http://www.infoq.com/articles/guardian_scala. Retrieved 2011-04-05. 
  25. ^ David Reid and Tania Teixeira (26 February 2010). "Are people ready to pay for online news?". BBC. http://news.bbc.co.uk/1/hi/programmes/click_online/8537519.stm. Retrieved 2010-02-28. 
  26. ^ Binstock, Andrew (2011-07-14). "Interview with Scala's Martin Odersky". Dr. Dobb's Journal. http://drdobbs.com/architecture-and-design/231001802. Retrieved 2012-02-10. 
  27. ^ Synodinos, Dionysios G. (2010-10-11). "LinkedIn Signal: A Case Study for Scala, JRuby and Voldemort". InfoQ. http://www.infoq.com/articles/linkedin-scala-jruby-voldemort. 

Further reading

External links

Personal tools
  • Log in / create account
Namespaces

Variants
Actions