Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Late evaluation in c#?

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 would like to set-and-forget. Once I schedule a part of my car in for a service, I want it to happen on an ongoing basis without any further input from me.
  • I don’t want to have to call each of my service centres and notify them the instant that I purchase a new car. In fact, they don’t need to know that I have a new car until it comes time for a service. If they try to service my car and it doesn’t exist, then it’s ok for them to throw an exception.
  • I want each of my services to be as simple and focussed as possible. Why give the tyre shop a reference to my entire car (including engine, interior, etc), when the only thing they really care about is the tyres?
  • I don’t want the tyreshop to give me a new set of tyres and force me to fit (assign) them to the car myself.

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;

  1. If I were to start again, what would be the best language to tackle such a problem in?
  2. Are there any existing platforms for c# that provide support for lazy evaluation?
  3. What are the alternatives for making this work in c#? Are there any particular design patterns I should read up on?
like image 502
knick Avatar asked Jan 16 '23 22:01

knick


1 Answers

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#.

like image 108
Jon Skeet Avatar answered Jan 28 '23 13:01

Jon Skeet