Constructor with non-instance variable assistant?
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. I开发者_运维问答n Java, I'd do something like this:
public class Foo {
final Derived derived1, derived2;
public Foo(BasicData val) {
super(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.
You could use a block to create a temporary helper val and return a tuple, like this:
class Foo(v: BasicData) extends Bar(v) {
val (derived1, derived2) = {
val helper = new Helper(v)
(helper.getDerived1Value(), helper.getDerived2Value())
}
}
Better look at the javap output (including private members) before you conclude this has side-stepped any fields for the Tuple2
used in the intermediate pattern-matching.
As of Scala 2.8.0.RC2, this Scala code (fleshed out to compile):
class BasicData
{
def basic1: Int = 23
def basic2: String = "boo!"
}
class Helper(v: BasicData)
{
def derived1: Int = v.basic1 + 19
def derived2: String = v.basic2 * 2
}
class Bar(val v: BasicData)
class Foo(v: BasicData)
extends Bar(v)
{
val (derived1, derived2) = {
val helper = new Helper(v)
(helper.derived1, helper.derived2)
}
}
Produces this Foo
class:
% javap -private Foo
public class Foo extends Bar implements scala.ScalaObject{
private final scala.Tuple2 x$1;
private final int derived1;
private final java.lang.String derived2;
public int derived1();
public java.lang.String derived2();
public Foo(BasicData);
}
精彩评论