Supriya Ghosh (Editor)

Scala (programming language)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Designed by
  
Martin Odersky

Scala (programming language) codescottshippcomwpcontentuploads201409635

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

and runs it with

$ scala HelloWorld

This 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:

$ scala HelloWorld2.scala

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:

  • Scala does not require semicolons to end statements.
  • Value types are capitalized: Int, Double, Boolean instead of int, double, boolean.
  • Parameter and return types follow, as in Pascal, rather than precede as in C.
  • Methods must be preceded by def.
  • Local or class variables must be preceded by val (indicates an immutable variable) or var (indicates a mutable variable).
  • The return operator is unnecessary in a function (although allowed); the value of the last executed statement or expression is normally the function's value.
  • Instead of the Java cast operator (Type) foo, Scala uses foo.asInstanceOf[Type], or a specialized function such as toDouble or toInt.
  • Instead of Java's import foo.*;, Scala uses import foo._.
  • Function or method 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 references are written like function calls, e.g. 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.)
  • Generic types are written as e.g. List[String] rather than Java's List<String>.
  • Instead of the pseudo-type 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:

  • Scala has no static variables or methods. Instead, it has singleton objects, which are essentially classes with only one object in the class. Singleton objects are declared using 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.)
  • In place of constructor parameters, Scala has class parameters, which are placed on the class, similar to parameters to a function. When declared with a 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.
  • Default visibility in Scala is 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

  • In April 2009, Twitter announced that it had switched large portions of its backend from Ruby to Scala and intended to convert the rest.
  • Gilt uses Scala and Play Framework.
  • Foursquare uses Scala and Lift.
  • SpinGo uses Scala and Akka.
  • Coursera uses Scala and Play Framework.
  • Apple Inc. uses Scala in certain teams, along with Java and the Play framework.
  • The Guardian newspaper's high-traffic website guardian.co.uk announced in April 2011 that it was switching from Java to Scala,
  • The New York Times revealed in 2014 that its internal content management system Blackbeard is built using Scala, Akka and Play.
  • The Huffington Post newspaper started to employ Scala as part of its contents delivery system Athena in 2013.
  • Swiss bank UBS approved Scala for general production usage.
  • The BitGold platform was built entirely on Scala and Play Framework.
  • LinkedIn uses the Scalatra microframework to power its Signal API.
  • Meetup uses Unfiltered toolkit for real-time APIs.
  • Remember the Milk uses Unfiltered toolkit, Scala and Akka for public API and real time updates.
  • Verizon seeking to make "a next generation framework" using Scala.
  • LeadIQ was built entirely on Scala, Akka and Play Framework.
  • Airbnb develops open source machine learning software "Aerosolve", written in Java and Scala.
  • Zalando moved its technology stack from Java to Scala and Play.
  • SoundCloud uses Scala for its back-end, employing technologies such as Finagle (micro services), Scalding and Spark (data processing).
  • Databricks uses Scala for the Apache Spark Big Data platform.
  • Morgan Stanley uses Scala extensively in their finance and asset-related projects.
  • There are teams within Google/Alphabet Inc. that use Scala, mostly due to acquisitions such as Firebase and Nest.
  • Walmart Canada Uses Scala for their back end platform.
  • x.ai uses Scala for their AI-driven Personal Assistant.
  • Duolingo uses Scala for their back-end module that generates lessons.
  • 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.

    References

    Scala (programming language) Wikipedia