Scala (programming language): Difference between revisions
imported>Yuvi Masory No edit summary |
imported>Hayford Peirce (moved TOC to remove white space) |
||
Line 1: | Line 1: | ||
{{subpages}} | {{subpages}} | ||
{{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]] implementation also exists, but is not as well maintained and has not seen significant use. |
Revision as of 17:07, 13 July 2010
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
Design goals
Java compatibility
Object orientation
Functional programming
Conciseness
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.[1]
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
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." [2]
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
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
Target audience and adoption
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.