I have this piece of code that loads Properties from a file:
class Config {
val properties: Properties = {
val p = new Properties()
p.load(Thread.currentThread().getContextClassLoader.getResourceAsStream("props"))
p
}
val forumId = properties.get("forum_id")
}
This seems to be working fine.
I have tried moving the initialization of properties
into another val, loadedProperties
, like this:
class Config {
val properties: Properties = loadedProps
val forumId = properties.get("forum_id")
private val loadedProps = {
val p = new Properties()
p.load(Thread.currentThread().getContextClassLoader.getResourceAsStream("props"))
p
}
}
But it doesn't work! (properties
is null in properties.get("forum_id")
).
Why would that be? Isn't loadedProps
evaluated when referenced by properties
?
Secondly, is this a good way to initialize variables that require non-trivial processing? In Java, I would declare them final
fields, and do the initialization-related operations in the constructor.
Is there a pattern for this scenario in Scala?
Thank you!
Vals are initialized in the order they are declared (well, precisely, non-lazy vals are), so
properties
is getting initialized beforeloadedProps
. Or in other words,loadedProps
is stillnull
whenproperties
is getting initialized. The simplest solution here is to defineloadedProps
beforeproperties
:You could also make
loadedProps
lazy, meaning that it will be initialized on its first access:Using lazy val has the advantage that your code is more robust to refactoring, as merely changing the declaration order of your vals won't break your code.
Also in this particular occurence, you can just turn
loadedProps
into adef
(as suggested by @NIA) as it is only used once anyway.Just an addition with a little more explanation:
Your
properties
field initializes earlier thanloadedProps
field here.null
is a field's value before initialization - that's why you get it. Indef
case it's just a method call instead of accessing some field, so everything is fine (as method's code may be called several times - no initialization here). See, http://docs.scala-lang.org/tutorials/FAQ/initialization-order.html. You may usedef
orlazy val
to fix itWhy
def
is so different? That's becausedef
may be called several times, butval
- only once (so its first and only one call is actually initialization of the fileld).lazy val
can initialize only when you call it, so it also would help.Another, simpler example of what's going on:
Talking more generally, theoretically scala could analize the dependency graph between fields (which field needs other field) and start initialization from final nodes. But in practice every module is compiled separately and compiler might not even know those dependencies (it might be even Java, which calls Scala, which calls Java), so it's just do sequential initialization.
So, because of that, it couldn't even detect simple loops:
Actually, such loop (inside one module) can be theoretically detected in separate build, but it won't help much as it's pretty obvious.
I think here
loadedProps
can be simply turned into a function by simply replacingval
withdef
:In this case you are sure that it is called when you call it.
But not sure is it a pattern for this case.