lihaoyi / hands-on-scala-js

Better documentation for Scala.js
142 stars 52 forks source link

As a Scala newbie, could you explain your usage of #28

Closed sixman9 closed 9 years ago

sixman9 commented 9 years ago

Apologies, this is not an issue per se, I'm just looking for a little clarification regarding some code usage.

I'm a Java dev tinkering with Scala and while looking at your Weather1.scala code I noticed the following, class-less, 'extends' code construct:

object Weather1 extends{
    //Class definition code goes here....
} //No 'with <class> usage'

After some searching I found 'Scala early definitions', which makes some sense to me in respect to pre-initialising super class fields, explicitly when extra 'with ' definitions are made at the end of the anonymous code block. Your Weather1 code makes no further reference to extra super classes (i.e. no 'with'), so could you explain why you make use of this extends-anonymous-class structure, thanks?

Quoting this stackoverflow post:

abstract class X {
    val name: String
    val size = name.size
}

class Y extends {
    val name = "class Y"
} with X

If the code were written instead as

class Z extends X {
    val name = "class Z"
}

then a null pointer exception would occur when Z got initialized, because size is initialized before name in the normal ordering of initialization (superclass before class).

lihaoyi commented 9 years ago

I think it's a typo. It shouldn't need to extends. Scala just happens to work fine with it and ignores it =P