Constructor with non-instance variable assistant?
Posted
by Robert Fischer
on Stack Overflow
See other posts from Stack Overflow
or by Robert Fischer
Published on 2010-05-22T17:21:42Z
Indexed on
2010/05/22
17:40 UTC
Read the original article
Hit count: 231
scala
|constructor
I have a number of classes that look like this:
class Foo(val:BasicData) extends Bar(val) {
val helper = new Helper(val)
val derived1 = helper.getDerived1Value()
val derived2 = helper.getDerived2Value()
}
...except that I don't want to hold onto an instance of "helper" beyond the end of the constructor. In Java, I'd do something like this:
public class Foo {
final Derived derived1, derived2;
public Foo(BasicData val) {
Helper helper = new Helper(val);
derived1 = helper.getDerived1Value();
derived2 = helper.getDerived2Value();
}
}
So how do I do something like that in Scala? I'm aware of creating a helper object of the same name of the class with an apply method: I was hoping for something slightly more succinct.
© Stack Overflow or respective owner