Designed by Martin Odersky | ||
![]() | ||
Paradigm Multi-paradigm: functional, object-oriented, imperative, concurrent Developer Programming Methods Laboratory of École Polytechnique Fédérale de Lausanne First appeared January 20, 2004; 13 years ago (2004-01-20) Stable release 2.12.1 / December 5, 2016; 3 months ago (2016-12-05) Typing discipline static, strong, inferred, structural |
Scala tutorial
Scala (/ˈskɑːlɑː/ SKAH-lah) is a general-purpose programming language providing support for functional programming and a strong static type system. Designed to be concise, many of Scala's design decisions were designed to build from criticisms of Java.
Contents
- Scala tutorial
- What is scala programming an introduction to methods classes functions and collections
- History
- Platforms and license
- Other Compilers and Targets
- Hello World example
- Basic example
- Example with classes
- Concurrency
- Cluster computing
- Testing
- Comparison with other JVM languages
- Language rankings
- Companies
- Criticism
- References
Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java virtual machine. Scala provides language interoperability with Java, so that libraries written in both languages may be referenced directly in Scala or Java code. Like Java, Scala is object-oriented, and uses a curly-brace syntax reminiscent of the C programming language. Unlike Java, Scala has many features of functional programming languages like Scheme, Standard ML and Haskell, including currying, type inference, immutability, lazy evaluation, and pattern matching. It also has an advanced type system supporting algebraic data types, covariance and contravariance, higher-order types (but not higher-rank types), and anonymous types. Other features of Scala not present in Java include operator overloading, optional parameters, named parameters, raw strings, and no checked exceptions.
The name Scala is a portmanteau of scalable and language, signifying that it is designed to grow with the demands of its users.
What is scala programming an introduction to methods classes functions and collections
History
The design of Scala started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) by Martin Odersky. It followed on from work on Funnel, a programming language combining ideas from functional programming and Petri nets. Odersky formerly worked on Generic Java, and javac, Sun's Java compiler.
After an internal release in late 2003, Scala was released publicly in early 2004 on the Java platform, A second version (v2.0) followed in March 2006.
Although Scala had extensive support for functional programming from its inception, Java remained a mostly object oriented language until the inclusion of lambda expressions with Java 8 in 2014.
On 17 January 2011 the Scala team won a five-year research grant of over €2.3 million from the European Research Council. On 12 May 2011, Odersky and collaborators launched Typesafe Inc. (later renamed Lightbend Inc.), a company to provide commercial support, training, and services for Scala. Typesafe received a $3 million investment in 2011 from Greylock Partners.
Platforms and license
Scala runs on the Java platform (Java virtual machine) and is compatible with existing Java programs. As Android applications are typically written in Java and translated from Java bytecode into Dalvik bytecode (which may be further translated to native machine code during installation) when packaged, Scala's Java compatibility makes it well suited to Android development, more so when a functional approach is preferred.
The reference Scala software distribution, including compiler and libraries, is released under a BSD license.
Other Compilers and Targets
Scala.js is a Scala compiler that compiles to JavaScript, making it possible to write Scala programs that can run in web browsers.
Scala Native is a Scala compiler that targets the LLVM compiler infrastructure to create executable code that uses a lightweight managed runtime which utilizes the Boehm garbage collector. The project is led by Denys Shabalin and had its first release , 0.1, on March 14, 2017. Development of Scala Native began in 2015 with a goal of being faster than Just-in-time compilation for the JVM by eliminating the initial runtime compilation of code and also providing the ability to call native routines directly.
A reference Scala compiler targeting the .NET Framework and its Common Language Runtime was released in June 2004, but was officially dropped in 2012.
"Hello World" example
The Hello World program written in Scala has this form:
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.
When the program is stored in file HelloWorld.scala, the user compiles it with the command
$ scalac HelloWorld.scalaand runs it with
$ scala HelloWorldThis is analogous to the process for compiling and running Java code. Indeed, Scala's compiling and executing model is identical to that of Java, making it compatible with Java build tools such as Apache Ant.
A shorter version of the "Hello World" Scala program is:
Scala includes interactive shell and scripting support. Saved in a file named HelloWorld2.scala
, this can be run as a script with no prior compiling using:
Commands can also be entered directly into the Scala interpreter, using the option -e:
$ scala -e 'println("Hello, World!")'Commands can be entered interactively in the REPL:
Basic example
The following example shows the differences between Java and Scala syntax:
Some syntactic differences in this code are:
Int, Double, Boolean
instead of int, double, boolean
.def
.val
(indicates an immutable variable) or var
(indicates a mutable variable).return
operator is unnecessary in a function (although allowed); the value of the last executed statement or expression is normally the function's value.(Type) foo
, Scala uses foo.asInstanceOf[Type]
, or a specialized function such as toDouble
or toInt
.import foo.*;
, Scala uses import foo._
.foo()
can also be called as just foo
; method thread.send(signo)
can also be called as just thread send signo
; and method foo.toString()
can also be called as just foo toString
.These syntactic relaxations are designed to allow support for domain-specific languages.
Some other basic syntactic differences:
array(i)
rather than array[i]
. (Internally in Scala, both arrays and functions are conceptualized as kinds of mathematical mappings from one object to another.)List[String]
rather than Java's List<String>
.void
, Scala has the actual singleton class Unit
(see below).Example with classes
The following example contrasts the definition of classes in Java and Scala.
The above code shows some of the conceptual differences between Java and Scala's handling of classes:
object
instead of class
. It is common to place static variables and methods in a singleton object with the same name as the class name, which is then known as a companion object. (The underlying class for the singleton object has a $
appended. Hence, for class Foo
with companion object object Foo
, under the hood there's a class Foo$
containing the companion object's code, and one object of this class is created, using the singleton pattern.)val
or var
modifier, fields are also defined with the same name, and automatically initialized from the class parameters. (Under the hood, external access to public fields always goes through accessor (getter) and mutator (setter) methods, which are automatically created. The accessor function has the same name as the field, which is why it's unnecessary in the above example to explicitly declare accessor methods.) Note that alternative constructors can also be declared, as in Java. Code that would go into the default constructor (other than initializing the member variables) goes directly at class level.public
.Concurrency
Scala standard library includes support for the actor model, in addition to the standard Java concurrency APIs. Lightbend Inc. provides a platform that includes Akka, a separate open source framework that provides actor-based concurrency. Akka actors may be distributed or combined with software transactional memory (transactors). Alternative communicating sequential processes (CSP) implementations for channel-based message passing are Communicating Scala Objects, or simply via JCSP.
An Actor is like a thread instance with a mailbox. It can be created by system.actorOf
, overriding the receive
method to receive messages and using the !
(exclamation point) method to send a message. The following example shows an EchoServer that can receive messages and then print them.
Scala also comes with built-in support for data-parallel programming in the form of Parallel Collections integrated into its Standard Library since version 2.9.0.
The following example shows how to use Parallel Collections to improve performance.
Besides actor support and data-parallelism, Scala also supports asynchronous programming with Futures and Promises, software transactional memory, and event streams.
Cluster computing
The most well-known open source cluster computing solution, written in Scala, is Apache Spark. Additionally, Apache Kafka, the publish-subscribe message queue popular with Spark and other stream processing technologies, is written in Scala.
Testing
There are several ways to test code in Scala. ScalaTest supports multiple testing styles and can integrate with Java-based testing frameworks. ScalaCheck is a library similar to Haskell's QuickCheck. specs2 is a library for writing executable software specifications. ScalaMock provides support for testing high-order and curried functions. JUnit and TestNG are popular testing frameworks written in Java.
Comparison with other JVM languages
Scala is often compared with Groovy and Clojure, two other programming languages also using the JVM. Substantial differences between these languages are found in the type system, in the extent to which each language supports object-oriented and functional programming, and in the similarity of their syntax to the syntax of Java.
Scala is statically typed, while both Groovy and Clojure are dynamically typed. This makes the type system more complex and difficult to understand but allows almost all type errors to be caught at compile-time and can result in significantly faster execution. By contrast, dynamic typing requires more testing to ensure program correctness and is generally slower in order to allow greater programming flexibility and simplicity. Regarding speed differences, current versions of Groovy and Clojure allow for optional type annotations to help programs avoid the overhead of dynamic typing in cases where types are practically static. This overhead is further reduced when using recent versions of the JVM, which has been enhanced with an invoke dynamic instruction for methods that are defined with dynamically typed arguments. These advances reduce the speed gap between static and dynamic typing, although a statically typed language, like Scala, is still the preferred choice when execution efficiency is very important.
Regarding programming paradigms, Scala inherits the object-oriented model of Java and extends it in various ways. Groovy, while also strongly object-oriented, is more focused in reducing verbosity. In Clojure, object-oriented programming is deemphasised with functional programming being the main strength of the language. Scala also has many functional programming facilities, including features found in advanced functional languages like Haskell, and tries to be agnostic between the two paradigms, letting the developer choose between the two paradigms or, more frequently, some combination thereof.
Regarding syntax similarity with Java, Scala inherits much of Java's syntax, as is the case with Groovy. Clojure on the other hand follows the Lisp syntax, which is different in both appearance and philosophy. However, learning Scala is also considered difficult because of its many advanced features. This is not the case with Groovy, despite its also being a feature-rich language, mainly because it was designed to be mainly a scripting language.
Language rankings
Scala was voted the most popular JVM scripting language at the 2012 JavaOne conference.
As of 2013, all JVM-based languages (Scala, Groovy, Clojure) are significantly less popular than the original Java language, which is usually ranked first or second, and which is also simultaneously evolving over time.
The RedMonk Programming Language Rankings, as of June 2016 placed Scala 14th, based on a position in terms of number of GitHub projects and in terms of number of questions tagged on Stack Overflow. (Groovy and Clojure were both in 20th place.) Here, Scala is shown somewhat between a first-tier group of languages (including, C, Python, PHP, Ruby, etc.), and ahead of a second-tier group.
Another measure, the Popularity of Programming Language Index which tracks searches for language tutorials ranked Scala 15th in July 2016 with a small upward trend, making it the most popular JVM-based language after Java.
As of January 2016, the TIOBE index of programming language popularity shows Scala in 30th place (as measured by internet search engine rankings and similar publication-counting), but–as mentioned under "Bugs & Change Requests"–TIOBE is aware of issues with its methodology of using search terms which might not be commonly used in some programming language communities. In this ranking Scala is ahead of functional languages Haskell (39th), Erlang (35rd) and Clojure (>50), but below Java (1st).
The ThoughtWorks Technology Radar, which is an opinion based half-yearly report of a group of senior technologists, recommends Scala adoption in its languages and frameworks category.
According to Indeed.com Job Trends, Scala demand has been rapidly increasing since 2010, trending ahead of Clojure and Groovy.
Companies
Criticism
In March 2015, former VP of the Platform Engineering group at Twitter Raffi Krikorian, stated he would not have chosen Scala in 2011 due to its learning curve. The same month, LinkedIn SVP Kevin Scott stated their decision to "minimize [their] dependence on Scala." In November 2011, Yammer moved away from Scala for reasons that included the learning curve for new team members and incompatibility from one version of the Scala compiler to the next.