tags:

views:

392

answers:

8

I would like a form a list of what nagging things of Java are history and by what feature of Scala has it been replaced.

I must admit I am new to Scala, so I cannot contribute much. But I feel, this list will be helpful for me as well as others to fully appreciate Scala

For e.g. we use keyword "val" and this makes the value immutable after initialization. In Java, we had to type the type of the object and also the keyword final. Scala frees us of this pain.

Concurrency support is obviously better in Scala, but I am not looking for that. That is too big a good feature to get ignored. I am in search for the finer details that even though miniscule will have a good effect.

Some areas are: Type systems, Exception handling, Regexes, OOPS features, syntactic sugar etc.

A: 

I like the fact that functions are now Objects, and that primitives are wrapped by default. The ability of the compiler to intelligently infer type is also nice.

Kaleb Brasee
A: 

I disagree with a few assumptions of the question.

Scala runs on the JVM, so concurrency support (at the VM level) cannot be better than Java. Mind you, actors may be a better model than Java threads, but I would be surprised if actors couldn't be implemented in Java.

Further, Java has Regular Expressions.

Chip Uni
...but so has Scala...
Banang
+5  A: 
  • Null is gone (mostly) in Scala
  • For expressions
  • Covariant and contravariant definition-side generics
  • infix operators/methods (a + b, list map f)
  • Pattern matching
  • implicit type conversions
  • static is gone (with object)
  • powerful package support (nested packages)

But as every feature they have their implications it's part of the design when to use:

  • for expression vs map/flatMap/filter/...
  • pattern matching vs polymorphism
  • infix vs method call syntax

When to use implicit type conversions and generics at all.

Nothing is for free. Scala is a powerful tool so you can hurt yourself.

Thomas Jung
+8  A: 

One thing I like is being able to write something like:

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

Instead of:

class Person {

  private String name;
  private int age;

  public Person(String name, int age) {
    this.name=name;
    this.age=age;
  }

  public String getName() {
      return name;
  }

  public int getAge{
      return age;
  }

  public String toString() { 
      return String.format("Person(%s, %d)", name, age);
  }

  public boolean equals(Object other) {
      if (other == this) 
          return true;
      if (other.getClass() != getClass())
          return false;
      Person p = (Person) other;
      return getName().equals(p.getName()) && getAge().equals(p.getAge());
  }

  public int hashCode() {
      int h = getName().hashCode();
      h = 37 * h + getAge(); //or whatever it is!
      return h;
  }
}
Fabian Steeg
You forgot the getters! Make it a `case class` and you also get `equals`, `hashCode` and `toString` in there as well!
oxbow_lakes
He didn't forget the setters. The `case class` only gives you `val`s by default.
Ken Bloom
But vals are effectively getters by virtue of Scala's Uniform Access Principle
Aaron Novstrup
But they are not setters.
Seun Osewa
+4  A: 

I think two of the major features of Scala are:

  • Static type inference - this cool language feature really allows you to make your code shorter but necessarily faster.
  • Variance annotations i.e. nonvariance (default in scala & java), co-variance & contra-variance - another feature that allows you to associated types in your application without writing too much code.

However, one thing that doesn't sit too well with me is the existence of implicit conversions (they are similar to C++'s implicit conversion) which sounds like a quick hack i.e. duct-taping

For your consideration

Raymond Tay
+4  A: 

Scala has a powerful concept for multiple inheritance: traits! That goes a long way to coming near to Eiffel's implementation inheritance. It also has some similarity with Ruby's mixin modules.

Robert Klemme
A: 

I'm looking forward to optionally named parameters in 2.8. They will facilitate self-documenting code on immutable objects since you can use the parameter names while invoking the constructor.

Also, the copy capabilities on case classes in 2.8 will make it easy to implement "copy on modify" objects much like the immutable collections already in Scala.

+1  A: 

Working on Scala 2.8 here, so some of this isn't yet fully released...

Scala especially helps with removing boilerplate:

  • Type Inference
  • Traits / Mixins
  • Uniform Access Principle - no more getters and setters
  • Operator Overloading

and adding functional constructs to further reduce the size of your program:

  • First-Class Functions - Yes, that means closures :)
  • For-Comprehensions. These are NOT loops, but a much more powerful beast.
  • match/case blocks - Pattern Matching, much more powerful than switch/case

It also works nicely around some of the design flaws in Java

  • Manifest class to recapture information lost by erasure
  • Singletons - Far more object-oriented than static methods on a class
  • Declaration-Site Variance - No more <? extends T>
  • No more checked exceptions

And a few nice extras

  • Named and Default parameters
  • implicit conversions and parameters
  • XML literals
  • Wiki syntax scaladoc

Also see http://www.artima.com/weblogs/viewpost.jsp?thread=275983 for an overview of some of the functional programming concepts

Kevin Wright