Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 1: A Scalable Language > 1.3 Why Scala? - Pg. 12

C HAPTER 1 · A Scalable Language whereas they are mutable in Ruby. So looking at just strings, Java is a func- tional language, whereas Ruby is not. Immutable data structures are one of the cornerstones of functional programming. The Scala libraries define many more immutable data types on top of those found in the Java APIs. For instance, Scala has immutable lists, tuples, maps, and sets. Another way of stating this second idea of functional programming is that methods should not have any side effects. They should communicate with their environment only by taking arguments and returning results. For instance, the replace method in Java's String class fits this description. It takes a string and two characters and yields a new string where all occur- rences of one character are replaced by the other. There is no other effect of calling replace . Methods like replace are called referentially transparent, which means that for any given input the method call could be replaced by its result without affecting the program's semantics. Functional languages encourage immutable data structures and referen- tially transparent methods. Some functional languages even require them. Scala gives you a choice. When you want to, you can write in an imper- ative style, which is what programming with mutable data and side effects is called. But Scala generally makes it easy to avoid imperative constructs when you want, because good functional alternatives exist. 1.3 Why Scala? Is Scala for you? You will have to see and decide for yourself. We have found that there are actually many reasons besides scalability to like programming in Scala. Four of the most important aspects will be discussed in this section: compatibility, brevity, high-level abstractions, and advanced static typing. Scala is compatible Scala doesn't require you to leap backwards off the Java platform to step for- ward from the Java language. It allows you to add value to existing code--to build on what you already have--because it was designed for seamless in- teroperability with Java. 8 Scala programs compile to JVM bytecodes. Their run-time performance is usually on par with Java programs. Scala code can 8 There is also a Scala variant that runs on the .NET platform, but the JVM variant cur- rently has better support. 12