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?
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).
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:
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:
If the code were written instead as
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).