rrickfox / traffic-sim

GNU General Public License v3.0
2 stars 0 forks source link

berechenbare properties #59

Closed funketh closed 4 years ago

funketh commented 4 years ago

derzeitige Lösung wäre:

class Test {
    private Vector2D pos1;
    private Vector2D pos2;
    private Lazy<float> _distance;
    public float distance => this._distance.Value;

    public Test(...) {
        ...
        this._distance = new Lazy<float>(() => Vector2D.Distance(pos1, pos2));
    }
}

gewünscht wäre sowas:

class Test {
    private Vector2D pos1;
    private Vector2D pos2;
    [Cached] public float distance => Vector2D.Distance(pos1, pos2);

    public Test(...) {
        ...
    }
}
funketh commented 4 years ago

ok, anscheinend ist es nicht so einfach solche macros zu implementieren (https://stackoverflow.com/questions/14307298/basic-implementation-of-aop-like-attribute-using-standard-net-framework). Man kann ja diese Form nutzen:

class Test {
    private Vector2D pos1;
    private Vector2D pos2;
    private Lazy<float> _distance = new Lazy<float>(() => Vector2D.Distance(pos1, pos2));
    public float distance => this._distance.Value;

    public Test(...) {
        ...
    }
}
funketh commented 4 years ago

das wäre wohl zu einfach gewesen, man kann in dem kontext nicht auf pos1 und pos2 zugreifen

funketh commented 4 years ago

Berechne jetzt in #58 die Werte einfach im Konstruktor. Funktioniert in dem Fall gut genug, weshalb ich das hier erstmal schließe.