News for hackers. Stuff that matters.
Re:Var and val in Java? by airless_cotton on Saturday March 26, 2016 @03:03PM (#11366933)
One aspect that the author mentions only in passing–but is interesting to look at in more detail–is to understand how Scala has solved this in a very consistent and satisfying fashion:

Instead of mixing "you can't override this in a subclass" and "you can't reassign this value" as Java did with final, it clearly separates these concerns:

  class Foo {
          var qux = 1.23
          val bar = 42
    final var fiz = "fiz"
    final val wow = "wow"
  }
  
  (new Foo).qux = 2.34  //   valid
  (new Foo).bar = 23    // invalid
  (new Foo).fiz = "fuz" //   valid
  (new Foo).wow = "wew" // invalid

  class SubFoo extends Foo  {
    override var qux = 2.34  //   valid
    override val bar = 23    //   valid
    override var fiz = "fuz" // invalid
    override val wow = "wew" // invalid
  }
Re:Var and val in Java? by deanCommie on Saturday March 26, 2016 @03:38PM (#11367087)
I know C# and Java, but not Scala.

That is NOT at all clear for me. Why is: (new Foo).fiz = "fuz" // valid?

fiz is Final! Why would you be allowed to re-assign a final variable on an instance??

Re:Var and val in Java? by bilalq on Saturday March 26, 2016 @03:59PM (#11367189)
Final does not mean immutable, but non-overridable through inheritance. The val keyword is what makes things immutable.