First let me say that when I use Scala, what I miss from Java is the
richness of the tool support.  Also, Scala has some limitations in the
kinds of annotations it can do.  But that's about it, and both are
being worked on actively.

What I like in Scala over Java, in no particular order:

Type inference.  I don't write nearly as many types in Scala as I do
in Java, but I still have a statically type checked program.  I can
write more types as needed for documentation.

Trait mixins.  A form of multiple inheritance without the diamond
inheritance problem.

Pattern matching.  Unifies the visitor pattern, switch statements, and
Java style serial catch blocks.    Flips the expression problem
around: OO style inheritance/overriding makes it easy to add a type to
a hiearchy, but hard to add an operation to an existing hierarchy
(without opening the code).  Pattern matching lets you do it the other
way around: easy to add an operation, hard to add a type without
opening the code and often that's the right answer.  Plus, pattern
matching can extract while it matches.  Hard to overstate how useful
this is.

Tuples.  Return multiple values from a function/method.  Deconstruct
using pattern matching.

Case classes.  Besides the relationship to pattern matching, they give
you automatic toString, equals, and hashCode implementations.

Self types.  A powerful way to create requirements on a class/trait.

Lambdas and closures.  Like anonymous inner classes with a fraction of
the cruft.

Higher kinded types.   Genericize not just types, but type
constructors.  There are things you can't express generically in
Java's static type system because it lacks this facility.

Definition site variance.  Don't need the equivalent of Foo<? extends
Bar> nearly as much since I can just declare Foo as being a covariant
type constructor ("generic type").

Implicits and viewbounds.  Like having support for the adapter pattern
in the language.  Also, allows an encoding of something like Haskell
type classes.

REPL. Great for exploratory stuff.

Uniform access principle.  Public fields automatically get getter (and
setter if mutable) functions while the underlying field gets hidden.

"For comprehensions."  Like a mixture of Java's enhanced for and
Haskell's "do" monad comprehensions.

Here's a little tiny piece of code that illustrates a great deal about
what Scala brings.  I urge you to write the equivalent in Java to get
a feel for the difference. (The stuff I typed goes in after the scala>
prompt.  Indented below that is the computer's response).

scala> case class Person(name : String, age : Int)

   defined class Person

scala> val people = List(Person("Bob", 28), Person("Sarah", 42), Person
("Toby", 12))

   people: List[Person] = List(Person(Bob,28), Person(Sarah,42), Person
(Toby,12))

scala> val (minors, adults) = people partition {_.age < 18}

   minors: List[Person] = List(Person(Toby,12))
   adults: List[Person] = List(Person(Bob,28), Person(Sarah,42))


On Dec 19, 3:31 am, Jan Goyvaerts <java.arti...@gmail.com> wrote:

> In the meantime I have a question for the experienced Scala people out
> there: What features of Scala are making you to prefer Scala over
> Java ? And why ?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to javaposse@googlegroups.com
To unsubscribe from this group, send email to 
javaposse+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to