January 8, 2013

Announcing Scala 2.10: A Simpler Way to Tackle Complex Problems

January 8, 2013

We are very excited about the first release in the Scala 2.10 series. This release makes it even simpler to tackle the tough challenges that developers face every day, ranging from the syntactic nicety of String Interpolation to expressing massive asynchronous computations in sequential code style.

Many new features enable library and framework developers to provide a simpler yet richer developer experience for their users. For example, the Play Framework harnesses Scala 2.10's new asynchronous programming model to compose services efficiently and concisely. Similarly, other library developers are using Scala 2.10's experimental Macros and Reflection features to further reduce complexity in applications. Even String Interpolation is easily extensible by library developers in Scala 2.10!

Here are some of Scala 2.10's new features that you can start using right now.


String Interpolation: Simple Type Safe String Substitution

Often times a developer needs to compose strings of text by substituting values for place holders. Scala 2.10 adds a new String Interpolation library that works with the power of compile-time type checking in Scala. Here are two examples.

To insert the contents of a variable in a string, simply prefix the string literal with an "s" and prefix the variable with a "$".

println(s"The word of the day is ‘$wordOfTheDay’.")

String interpolation also supports arbitrary expressions when wrapped with braces. Using printf style formatting, the string interpolator executes the expression and enforces type safety.

// log only two digits after the decimal to display as seconds"Time elapsed: ${millisElapsed/1000.0}%.2fs.")

The Scala IDE now supports Scala expressions in String Interpolations.


Futures & Promises: A Concise Async Programming Model

The Scala team at EPFL and the Akka team collaborated on a new API which standardizes the API for asynchronous programming using the Future and Promise paradigms. With valued input from Twitter and many other developers building massively asynchronous systems, the result is a very convenient API for expressing powerful asynchronous computations. Additionally, Akka's actors are now included in the distribution.

Developers who are building complex integration apps with Play and Akka are raving about the new Scala 2.10 Future API because it allows them to fire off hundreds of HTTP calls, then compose the results and render a web page. Composing numerous asynchronous calls in Scala 2.10 using a for-comprehension provides a concise programming model that feels natural but also manages resources efficiently and deals with errors gracefully. For instance, an asynchronous request handler in Play, that makes three asynchronous requests to render a page, may look like:

def profile(user: User) = Action {
      Async {
        for {
          lastPostId <- user.fetchLastPostId
          posts      <- fetchPosts(user, lastPostId)
          avatar     <- user.fetchAvatar
        } yield Ok(views.html.profile(user, posts, avatar))

Under the covers, Akka, Scala, and Play all work together to efficiently manage concurrency and resource utilization while maintaining a simple and concise programming model.


Value Classes: Objects On a Diet

Enhancing classes to include additional functionality while avoiding runtime overhead is now possible in Scala 2.10 with Value Classes. For instance, if you need to be able to convert every Int to its hexadecimal representation, then a Value Class can do this without having to create unnecessary instances. For example, the following RichInt class is a Value Class that allows you to wrap an Int to add a toHexString method, without the overhead of allocating an extra object.

class RichInt(val self: Int) extends AnyVal {
 def toUpperHexString: String = java.lang.Integer.toHexString(self).toUpperCase}

In the following example the new RichInt is automatically optimized by Scala so that the underlying instance stays in Int! This provides faster execution and less memory usage.

val x = new RichInt(2) // at run time, x is simply an integer!

Scala also provides a way to automatically use Value Classes. For instance, to make it so all Int classes can be used as a RichInt simply make the RichInt Value Class implicit:

implicit class RichInt(val self: Int) extends AnyVal { … }
2.toUpperHexString // no explicit wrapping required anymore!

Together with Implicit Classes, Value Classes make it simple to enhance objects in a type safe and concise way!

Even More: Scala 2.10 is Simply Better

  • Scala now provides a way to control different levels of language features so that those just learning to swim don't get thrown into the deep end of the pool. We've come up with a way of putting you in control of which of the features your codebase relies on. This simplifies the initial experience that developers have with Scala and allows organizations to scale their use of the language.
  • The pattern matcher was rewritten from scratch to produce more robust code and provide more user-friendly error messages.
  • The Dynamic trait provides a convenient construct for using dynamically typed objects when it makes sense (for example, accessing untyped data over REST).
  • The bytecode generator now uses the industry-standard ASM library to generate bytecode much faster and for a wider variety of JDK versions including the Java 1.5, 1.6 and 1.7 versions.
  • Scaladoc now shows implicitly added members, inheritance diagrams and member grouping. Check out the Future documentation to see it in action!
  • Scala 2.10 includes a few experimental features, including Macros and Reflection. While these are expected to change in the Scala 2.11 series, it hasn't stopped people from doing very cool things with them!


At Typesafe we are very excited about the Scala 2.10 release and everything it does to make developers more productive and apps more efficient! You can try out Scala 2.10.0 by downloading the standalone distribution (which includes the REPL) or get started in Eclipse using the Scala Eclipse Plugin. We think Scala 2.10 sets a new standard for building highly sophisticated and amazing apps in a simple, concise manner; we think you'll agree. More power less code!

By Adriaan Moors - Scala Tech Lead at Typesafe.

comments powered by Disqus
Browse Recent Blog Posts