Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here's how it works. When I say:

  class Kitty {
    val purrVolume: Int;
  }
What Scala actually produces in Java bytecodes is something like this Java equivalent:

  public class Kitty {
    private int purrVolume;
    public void setPurrVolume(int purrVolume) {
      this.purrVolume = purrVolume;
    }
    public int getPurrVolume() { return purrVolume; }
  }
Later, if you decide that you need to change it to do some special calculations in the getters and setters, you can add that to the Scala code. External users of the class will not need to be recompiled, because under the covers Scala was never actually exposing the public member variable.


I think you mean var, not val.

If you were using val you would do:

  class Kitty(val purrVolue: Int)
which would turn out to be in Java:

  public class Kitty {
    private final int purrVolume;
    public Kitty(final int purrVolume) {
       this.purrVolume = purrVolume;
    }
    public int getPurrVolume() { return purrVolume; }
  }


On the Java version you forgot equals() and hashcode(). Scala's case classes give you that for free.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: