开发者

A list of things from Java that we will now happily bid good-bye after Scala

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 b开发者_JAVA百科etter 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.


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;
  }
}


  • 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.


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


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.


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜