views:

2060

answers:

13

What makes Scala such a wonderful language, other than the type system? Almost everything I read about the language brings out 'strong typing' as a big reason to use Scala, but there has to be more than that. What are some of the other compelling and/or cool language features that make Scala a really useful tool?

+4  A: 

Supposedly it's very easy to make Scala code run concurrently on multiple processors.

Chase Seibert
+3  A: 

Scalability

Nathan Ross Powell
How can a language be scalable - does it write code for you ? Scala and java both have the same libs available, therefore you can leverage the same in both...
mP
+21  A: 

Here are some of the things that made me favour Scala (over, say, usual Java):

a) Type inference. The Java way of doing it:

Map<Something, List<SomethingElse>> list = new HashMap<Something, List<SomethingElse>>()

.. is rather verbose compared to Scala. The compiler should be able to figure it out if you give one of these lists.

b) First-order functions. Again, this functionality can be emulated with classes, but it's ugly.

c) Collections that have map and fold. These two tie in with (b), and also these two are something I wish for every time I have to write Java.

d) Pattern matching and case classes.

e) Variances, which mean that if S extends T, then List[S] extends List[T] as well.

Throw in some static types goodness as well, and I was sold on the language quite fast.

andri
XML is an integral part of Scala. XML is legal syntax within the language. For example:def addressNode(line1 : String,line2 : String, city : String, state : String, country : String) = {<address> <line>{line1}</line> {if (line2 != null) <line>{line2}</line> else Text("")} <city>{city}</city> <state>{state}</state> <country>{country}</country> </address>}http://blog.lostlake.org/index.php?/archives/26-5-Things-a-Java-developer-needs-to-know-about-Scala.html
Gastoni
This inline XML feature is just so cool, +1 for pointing it out.
Zsolt Török
+1  A: 

Here's a few fairly in depth explanations for the appeal of functional languages.

http://stackoverflow.com/questions/474497/how-why-do-functional-languages-specifically-erlang-scale-well/474594

Spencer Ruport
I'm actually a Ruby guy, mostly because I spent a *long* time with Perl, C, and a surprising amount of Lisp, so I'm already very sold on functional programming. :)
Don Werve
+12  A: 

I'm new to Scala, but my impression is:

Really good JVM integration will be the driving factor. JRuby can call java and java can call JRuby code, but it's explicitly calling into another language, not the clean integration of Scala-Java. So you can use Java libraries, and even mix and match in the same project.

I started looking at scala when I had a realization that the thing which will drive the next great language is easy concurrency. The JVM has good concurrency from a performance standpoint. I'm sure someone will say that Erlang is better, but Scala is actually usable by normal programmers.

Where Java falls down is that it's just so painfully verbose. It takes way too many characters to create and pass a Functor. Scala allows passing functions as arguments.

It isn't possible in Java to create a union type, or to apply an interface to an existing class. These are both easy in Scala.

Static typing usually has a big penalty of verboseness. Scala eliminates this downside while still giving the upside of static typing, which is compile time type checking, and it makes code assist in editors easier.

The ability to extend the language. This has been the thing that has kept Lisp going for decades, and that allowed Ruby on Rails.

Kevin Peterson
+2  A: 

Expressiveness of control flow. For example, it's very common to have a collection of data which you need to process in some way. This might be a list of trades in which the processing involves grouping by some properties (the currencies of the investment instruments) and then doing a summation (to get totals-per-currency perhaps).

In Java this involves separating out a piece of code to do the grouping (a few lines of for-loop) and then another piece of code to do the summation (another for loop). In Scala, this type of thing is typically achievable in one line of code using functional programming and then folding, which reads very expressively l-to-r.

Of course, this is just an argument for a functional language over Java.

oxbow_lakes
+6  A: 

Just shortly:

  • You get the power and platform-independency of the Java libraries, but without the boilerplate and verbosity.
  • You get the simplicity and productivity of Ruby, but with static typing and compiled bytecode.
  • You get the functional goodnesses and concurrency support of Haskell, but without complete paradigm shift and with the benefits of object-orientation.

What I find especially attractive in all of its magnificient features, among others:

  • Most of the object-oriented design patterns which require loads of boilerplate code in Java are supported natively, e.g. Singleton (via objects), Adapter, Decorator (via traits and implicits), Visitor (via pattern matching), Strategy (via closures) etc.
  • You can define your domain models and DSLs very concisely, then you can extend them with the necessary features (notification, association handling; parsing, serialization), without the need of code generation or frameworks.
  • And finally, there is full interoperability with the well-supported Java platform. You can mix Java and Scala in both directions. There is not much penalty nor compatibility problems when switching to Scala after having experienced the annoyances of Java which make code hard to maintain.
thSoft
+15  A: 

It's a mash up of the best bits from a bunch of languages, what's not to love:

  • Ruby's terse syntax
  • Java's performance
  • Erlang's Actor Support
  • Closures/Blocks
  • Convenient shorthand for maps & arrays
Michael
+2  A: 

I want to add the multi-paradigm (OO and FP) nature gives Scala an edge over other languages

GClaramunt
+4  A: 

Two killer features of Scala that I didn't see posted yet:

In effect, these features let you approximate (and in some ways surpass) Haskell's type classes.

Apocalisp
+1  A: 

If we abandon feature discussion and will talk about style, i would say it's pipe-line style of coding. You start from some object or collection, types dot and property or dot and transformation and do it until you form desired result. This way it's easy to write a chain of transoformations that will be easy to read them also. Traits to some extend will also allow you to apply the same approach to constructing types.

Alexey
+7  A: 

Scala is often paraded for having closures and implicits. Not surprising really, as lack of closures and explicit typing are perhaps the two biggest sources of Java boilerplate!

But once you examine it a little deeper, it goes far beyond Java-without-the-annoying bits, Perhaps the greatest strength of Scala is not one specific named feature, but how successful it is in unifying all of the features mentioned in other answers.

Post Functional

The union of object orientation and functional programming for example: Because functions are objects, Scala was able to make Maps implement the Function interface, so when you use a map to look up a value, it's no different syntactically from using a function to calculate a value. In unifying these paradigms so well, Scala truly is a post-functional language.

Or operator overloading, which is achieved by not actually having operators, they're just methods used in infix notation. So 1 + 2 is just calling the + method on an integer. If the method was named plus instead then you'd use it as 1 plus 2 which is no different from 1.plus(2). This is made possible because of another combination of features; everything in Scala is an object, there are no primitives, so integers can have methods.

Other Feature Fusion

Type classes were also mentioned, achieved by a combination of higher-kinded types, singleton objects, and implicits.

Other features that work well together are case classes and pattern matching, allowing you to easily build and deconstruct algebraic data types, without having to manually write all the tedious equality, hashcode, constructor and getter/setter logic that Java demands.

Specifying immutability by default, offering lazy values, and providing first class functions all combine to give you a language that's very suited to building efficient functional data structures.

The list goes on, but I've been using Scala for over 3 years now, and I'm still amazed almost daily at how well everything just works together.

Efficient and Versatile

Scala is also a small language, with a spec that (surprisingly!) only needs to be around 1/3 the size of Java's. This is partly because Java has a lot of special cases in the spec that Scala simplifies away, partly because of removing features such as primitives and operators, and partly because a lot of functionality has been moved from the language and into the libraries.

As a benefit of this, all the techniques available to the Scala library authors are also available to any Scala user, which makes it a great language for defining your own control-flow constructs and for building DSLs. This has been used to great effect in projects like Akka - a 3rd-party Actor framework.

Deep

Finally, it scales the full range of programming styles.

The runtime interpreter (known as the REPL) allows you to very quickly explore ideas in an interactive session, and Scala files can also be run as scripts without needing explicit compilation. When coupled with type inference, this gives Scala the feel of a dynamic language such as Ruby, Perl, or a bash script.

At the other end of the spectrum, traits, classes, objects and self-types allow you to build a full-scale enterprise system based on distinct components and using dependency injection without the need of 3rd-party tools. Scala also integrates with Java libraries at a level almost on-par with native Java, and by running on the JVM can take advantage of all the speed benefits offered on that platform, as well as being perfectly usable in containers such as tomcat, or with OSGi.

Kevin Wright