I know there are multiple questions addressing related problems, but I’m not sure it does attack exactly what I’m looking for. I’m still new to Scala, after several years of Java development. I’m looking for the best way to test if an object has been initialized, and if not, initialize it then. For example, in Java:
private MyObject myObj = null;
and at some point in the future:
public void initMyObj(){
if (myObj == null){
myObj = new MyObj();
}
// do something with myObj
}
After this, I might reassign myObj to a different object, but it is unlikely. In Scala, I have this:
class Test {
var myObj: MyObj = _
}
I’ve read that I could use Option instead, something like:
var myObj = None : Option[MyObj]
and then my check:
myObj match {
case None => ...
case Some(value) => ...
}
but it feels ackward to use this pattern when I might not make this kind of check anywhere else at any other time – though being so new to Scala, I might be wrong. Is this the best way to achieve what I want or is there any other option not involving Option?
It is not generally ideal practice in Scala to leave partially-constructed objects lying around. You would normally rethink how your objects were getting instantiated to see if you can’t use a different pattern that is less fragile. For example, instead of setting uninitialized variables in methods:
you would attempt to restructure your code to generate the values you need on demand, and delay the instantiation of foo until then:
Because Scala has easy access to tuples (and type inference), this can be a lot less painful than in Java.
Another thing you can do is defer the late initialization to someone else.
Now you pass in something that will make parameter b, and arrange for it to handle any late initialization stuff that needs to be handled. This doesn’t always avoid needing to set vars, but it can help push it out of your class code into your initialization logic (which is usually a better place for it).
Doing this with vars is a little less straightforward. Realistically, you’re probably best off just devoting a class to it e.g. by:
where you then (sadly) have to use
()on every read and write.Then you use an instance of this class instead of the actual
varand pass through the lazy initializer. (lazy valis very much like this under the hood; the compiler just protects you from noticing.)Finally, if you want to have a fully-functional object that is occasionally missing a value,
var x: Option[X]is the construct you want to use; if you can’t find a way around the standard Java creation patterns (and you don’t want to try something more exotic like objects that create each other with more and more information, either because performance is critical and you can’t afford it, or you dislike writing that much boilerplate to allow type-checking to verify that your object is properly created) but you otherwise want to use it,var x: X = nullis what I’d choose, not_. IfXis a primitive, you probably need to choose the correct value wisely anyway (for example,Double.NaNinstead of0.0,-1rather than0forInt) to indicate I-am-not-initialized. If it’s generic code and you wantAnyinstead ofAnyRef,asInstanceOf-ing back and forth betweenAnyandAnyRefis probably the best way out of poorly typechecked situation (assuming you really, really can’t useOption, which at that point is much clearer).