Scala (programming language): Difference between revisions
imported>Yuvi Masory |
Pat Palmer (talk | contribs) |
||
Line 2: | Line 2: | ||
{{TOC|right}} | {{TOC|right}} | ||
'''Scala''' (pronounced /skala/) is a statically typed hybrid functional/object-oriented programming language. It compiles to the [[Java Virtual Machine]] (JVM) and has full access to Java classes and libraries. Scala combines aspects of functional languages such as Haskell and Erlang with the object systems of more mainstream languages such as Java and C#. A [[.NET]] implementation also exists, but is not as well maintained and has not seen significant use. | '''Scala''' (pronounced /skala/) is a statically typed hybrid functional/object-oriented programming language. It compiles to the [[Java Virtual Machine]] (JVM) and has full access to Java classes and libraries. Scala combines aspects of functional languages such as Haskell and Erlang with the object systems of more mainstream languages such as Java and C#. A [[.NET Framework|.NET]] implementation also exists, but is not as well maintained and has not seen significant use. | ||
== History == | == History == |
Latest revision as of 14:45, 11 August 2024
![](http://s9.addthis.com/button1-share.gif)
Scala (pronounced /skala/) is a statically typed hybrid functional/object-oriented programming language. It compiles to the Java Virtual Machine (JVM) and has full access to Java classes and libraries. Scala combines aspects of functional languages such as Haskell and Erlang with the object systems of more mainstream languages such as Java and C#. A .NET implementation also exists, but is not as well maintained and has not seen significant use.
History
Name and logo
The name Scala is intended as portmanteau of scalable and language. Additionally, Scala means stairway in Italian, symbolizing what the designers consider an ascension to a superior programming language. A staircase is prominently featured on the cover of Programming in Scala. The Scala logo is an abstraction of a staircase in EPFL.[1]
Design goals
Java compatibility
Scala is designed to be interoperable with Java without any wrappers, boilerplate, glue code, or special modes. Some Scala syntax is even designed to superficially resemble similar Java constructions to promote a sort of esthetic contiguity. For example, Scala singletons, both in their access syntax and capitalization conventions, mimic Java static members.
Object orientation
Scala aims for the pure object-orientation of languages like Smalltalk. Unlike in Java which allows primitive types and arrays, in Scala every value is an object. Despite this goal, not all objects behave similarly in Scala. For example, value types (descendants of AnyVal) are declared both "abstract" and "final," something not available to client code. The behavior of the null object is dictated directly by the specification.
Functional programming
In the tradition of functional languages, nearly all statements are expressions in Scala, meaning they result in values. while-loops always result in the Unit value, and are only included as a compromise to imperative programming. for-comprehensions however can result in interesting values, namely collections.
Conciseness
Scala aims to preserve static typing while eliminating the boilerplate and verbosity of Java. Language features like case classes and automatic properties greatly reduce the need for getters and setters, a common source of boilerplate in Java. Class arguments bring construction arguments into the largest scope of a class template, eliminating another common source of boilerplate, copying constructor arguments into an outside scope.
Expressivity
Design trade-offs
In order to maintain compatibility with existing Java libraries Martin Odersky made certain concessions in the design of Scala. He has specifically cited the inclusion of a null reference, the use of static operator overloading, and the existence of classes (as opposed to traits alone) as necessary compromises.[2]
Inclusion of a null reference
Many programming languages, Java included, contain a null reference, which represents the value of a reference that points to no object. Since null in Java is compatible with any reference type, the compiler cannot assist in avoiding NullPointerExceptions (calling methods on null) leading to many errors that only present at run time. To retain Java compatibility Scala also possesses a null reference. Unlike Java's null Scala's is treated as an object, the only instance of the Null type which the compiler treats as a subtype of all AnyRef-derived types (equivalent to Java reference types). In practice Scala's null object is used only for Java compatibility. Scala prefers use of the Nothing type (treated by the compiler as a subtype of all types) instead. Since Nothing has no instances at all there is no possibility of null pointer-like exceptions. In practice Scala programs use the Option class to represent possibly non-existent values. An Option object can be either Some[X] if the value exists, or None if it does not.
Experimental features
Odersky was interested in adding certain experimental features to Scala, such as dynamic resolution method overloading, and the unification of traits with classes. The desire to maintain seamless compatibility with Java was a major factor in deciding against those experiments.
Large number of static types
Scala has a notably large number of static types. Steve Yegge referred to Scala's type system as "Frankenstein's Monster." [3]
Tail-call optimizations
Because the JVM does not fully support tail-call optimizations, the Scala compiler cannot always rewrite tail-call functions as loops. This kind of optimization is standard in purely functional languages such as Scheme. The compiler can generally optimize methods and local functions performing directly recursive calls. However, in other cases such as indirect recursion and recursive function values, the optimization is not available. Scala 2.8 introduces the @tailrec annotation to allow programmers to verify which of their tail-recursive functions are being optimized by the compiler.
Type erasure
Scala adopts the type erasure model of Java rather than type reification from .NET. Manifests, introduced in version 2.8, are able to detect parameterized types in only limited cases.
Use from Java
While it is easy to use Java from Scala, the converse can be difficult or impossible due to unique Scala features that have no Scala equivalent. The Scala Language Specification generally does not mandate implementation strategies, so Java code that accesses Scala risks software rot as the reference implementations evolves. As of Scala 2.8.0 it is possible for Java code to extend Scala classes, but generally impossible to implement Scala traits. Scala singletons can also be used via a static variable in the resulting JVM class file.
Documentation
Scala has a formal specification written by Martin Odersky. It is kept up to date with each new release of the reference implementation. At least four books on Scala have been published in English, with several more slated for a 2010 release. Additionally, many articles are available on Scala's official web page. Despite these resources Scala is still not as well documented as older languages like Java. Mid-level documentation is particularly lacking. For example, Programming in Scala (Odersky et al), the most comprehensive of the books written on Scala to date, does not cover language features such as structural typing and higher-kinded types.
Standard library
Notable third-party libraries
Web
Lift is a Scala framework for developing web applications.
Testing
ScalaTest provides a unit testing framework for Scala that is more feature-rich than using JUnit from Scala. Both ScalaTest and specs support behavior driven development. Originally a port of Haskell's QuickCheck, the ScalaCheck framework supports property-based testing.
Target audience and adoption
Many aspects of Scala's design are intended to make it possible for Java and C# programmers to transition. In that sense some see Scala as Java's successor on the JVM. Others, in a debate that in many mimics the closures controversy in Java 7, argue that Scala's fundamental design is too complex and too unconstrained to be a reasonable replacement for Java.
Scala has seen a handful of notable adoptions in industry. Twitter rewrote its back-end in Scala citing problems with Ruby's performance, lack of static typing, and lack of truly parallel threading.
Prime number example
/** Print prime numbers less than 100, very inefficiently */
object primes extends Application {
def isPrime(n: Int) = (2 until n) forall (n % _ != 0)
for (i <- 1 to 100 if isPrime(i)) println(i)
}
This example code shows a number of features of the language, including the cross-over between the object-orientated and functional styles. An object is being defined rather than a class, following the Singleton pattern. The object uses the Application trait to turn it into an executable script. It defines a method on the primes object called isPrime which takes one Int object (Scala wraps Java's built-in int type in an object with an upper-case first character) and iterates on all numbers from two through n (the "(2 until n)" constructs a list of all those numbers), and then calculates if it is prime by dividing n by that number and seeing if it returns zero. forall is being called as a method on the array constructed by the first bracketed segment in the method definition, and the second bracketed segment contains the loop - an anonymous or lambda function. The underscore character stands in for the current iteration. The for loop shows how iteration is conducted in Scala. println at the end of the line is a call to the System.out.println method in the Java library.