I have a Dart class with multiple fields that have to be final, because the class extends another class marked with @immutable.
The values of these fields are supposed to be calculated when an instance of the class is created. In Dart, "final instance variables must be initialized before the constructor body starts" (from dartlang.org).
In that scope, you can only call static methods.
That works for me except some fields depend on the same calculation, meaning that the same calculation is done twice. Is there any way to avoid that, i.e. by saving some temporary result?
My current code:
class _IntegralCurve extends Curve {
static double delta = 0.01;
_IntegralCurve(this.original) :
integral = calculateIntegral(original),
values = calculateNormalizedValues(original);
final Curve original;
final double integral; // Accessible to other classes.
final Map<double, double> values;
/// Does the actual integrating work. Called twice.
static Map<double, double> integrate(Curve original) {
double integral = 0.0;
final values = Map<double, double>();
for (double t = 0.0; t <= 1.0; t += delta) {
integral += original.transform(t) * delta;
values[t] = integral;
}
values[1.0] = integral;
return values;
}
/// Calculates the integral.
static double calculateIntegral(Curve curve) => integrate(curve)[1];
/// Calculates cumulative values.
static Map<double, double> calculateNormalizedValues(Curve curve) {
final values = integrate(curve);
for (final double t in values.keys) {
values[t] = values[t] / values[1];
}
return values;
}
double transform(double t) {
for (final key in values.keys)
if (key > t)
return values[key];
return values[1.0];
}
}