I am working on an application in c#. To make this application work, I have found myself doing some things that feel quite unnatural for the language I have selected. After going through many refactorings and refinements, I have come to the realization that the functionality that I am trying to implement is really a form of ‘lazy evaluation’ (I think). This is what I would like...
// Criteria 1: Lazy evaluation of expressions
int x = LazyEvaluated.New<int>();
int y = LazyEvaluated.New<int>();
int z = LazyEvaluated.New<int>();
z.Assign(x + y);
Assert.ThrowsException(z.Evalutate());
x.Assign(1);
Assert.ThrowsException(z.Evalutate());
y.Assign(2);
Assert.Equals(3, z.Evaluate());
x.Assign(3);
Assert.Equals(5, z.Evaluate());
// Criteria 2: Referencing relative to parent object
Car myCar = LazyEvaluated.New<Car>();
Engine engineInMyCar = LazyEvaluated.New<Engine>();
double displacementOfMyEngine = LazyEvaluated.New<double>();
engineInMyCar = myCar.Engine;
displacementOfMyEngine = engineInMyCar.Displacement;
Car subaru = new Car(new FlatFourEngine());
Car falcon = new Car(new InlineSixEngine());
myCar.Assign(subaru);
Assert.IsTypeOf<FlatFourEngine>(engineInMyCar.Evaluate());
Assert.IsEqual(2.0, displacementOfMyEngine.Evaluate());
myCar.Assign(falcon);
Assert.IsTypeOf<InlineSixEngine>(engineInMyCar.Evaluate());
Assert.IsEqual(4.0, displacementOfMyEngine.Evaluate());
And these are the simple class definitions that I have used for illustration...
public class Car {
private readonly Engine engine;
public Car(Engine engine) { this.engine = engine; }
public Engine Engine { get { return engine; } }
}
public abstract class Engine {
public abstract double Displacement { get; }
}
public class FlatFourEngine : Engine {
public override double Displacement { get { return 2.0; } }
}
public class InlineSixEngine : Engine {
public override double Displacement { get { return 4.0; } }
}
The reason I want this functionality is mostly for separation-of-concerns. To illustrate, let’s take the car analogue a bit further. My car is made up of lots of bits; engine, tyres, interior etc. My tyres need to be changed every 2 years by a tyre shop. My engine needs to have an oil change every 6 months by a mechanic. The interior needs to be vacuumed every 3 months by a detailer. It would be nice to able to do something along the lines of...
tyreShop.ReplaceTyres(myCar.Tyres, every2Years);
mechanic.ChangeOil(myCar.Engine, every6Months);
detailer.VacuumInterior(myCar.Interior, every3Months);
Some reasons for this approach;
I do not have any formal education in CS, and my only programming experience is with c & c# languages. I’ve just spent the last 6 months getting familiar with c# & .Net. For the most part, I really like the language and early-evaluation in general. But I am now questioning whether I would have been better off tackling my particular problem in a different language with inbuilt support for lazy evaluation.
My questions are;
It sounds like what you're really after is scheduling an action to take place later, repeatedly. I'd suggest that delegates - probably using lambda expressions - are the most appropriate approach for this:
scheduler.Schedule(every3Months, () => tyreShop.ReplaceTyres(myCar));
This way the only piece of your code which needs to know about the laziness aspect is the scheduler itself. All the rest of your code can assume that when you call a method, you want that action to take place right now, which is much more idiomatic C#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With