Hacker News new | ask | show | jobs
by airless_cotton 3735 days ago
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
  }
1 comments

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??

Final does not mean immutable, but non-overridable through inheritance. The val keyword is what makes things immutable.