Note that there are some explanatory texts on larger screens.

plurals
  1. POLate evaluation in c#?
    text
    copied!<p>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...</p> <pre><code>// Criteria 1: Lazy evaluation of expressions int x = LazyEvaluated.New&lt;int&gt;(); int y = LazyEvaluated.New&lt;int&gt;(); int z = LazyEvaluated.New&lt;int&gt;(); 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&lt;Car&gt;(); Engine engineInMyCar = LazyEvaluated.New&lt;Engine&gt;(); double displacementOfMyEngine = LazyEvaluated.New&lt;double&gt;(); engineInMyCar = myCar.Engine; displacementOfMyEngine = engineInMyCar.Displacement; Car subaru = new Car(new FlatFourEngine()); Car falcon = new Car(new InlineSixEngine()); myCar.Assign(subaru); Assert.IsTypeOf&lt;FlatFourEngine&gt;(engineInMyCar.Evaluate()); Assert.IsEqual(2.0, displacementOfMyEngine.Evaluate()); myCar.Assign(falcon); Assert.IsTypeOf&lt;InlineSixEngine&gt;(engineInMyCar.Evaluate()); Assert.IsEqual(4.0, displacementOfMyEngine.Evaluate()); </code></pre> <p>And these are the simple class definitions that I have used for illustration...</p> <pre><code>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; } } } </code></pre> <p>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...</p> <pre><code>tyreShop.ReplaceTyres(myCar.Tyres, every2Years); mechanic.ChangeOil(myCar.Engine, every6Months); detailer.VacuumInterior(myCar.Interior, every3Months); </code></pre> <p>Some reasons for this approach;</p> <ul> <li>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.</li> <li>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.</li> <li>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?</li> <li>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.</li> </ul> <p>I do not have any formal education in CS, and my only programming experience is with c &amp; c# languages. I’ve just spent the last 6 months getting familiar with c# &amp; .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.</p> <p>My questions are;</p> <ol> <li>If I were to start again, what would be the best language to tackle such a problem in?</li> <li>Are there any existing platforms for c# that provide support for lazy evaluation?</li> <li>What are the alternatives for making this work in c#? Are there any particular design patterns I should read up on?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload