Note that there are some explanatory texts on larger screens.

plurals
  1. POLate evaluation in c#?
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CO+1 for a very well formed question. About the matter at hand, I think "shops" can and should be given a reference to the entire car: if you need a tyre change, you bring your car to the shop, you don't usually take the tyres down yourself and go change them :) Also, if your "shops" handle a `Car` reference, you can make up a `Shop` superclass for them to inherit from, which can handle common tasks (thus allowing you to DRY). Not to mention (to keep close to your example) this allows to interesting behavior, like shops passing around your `Car` to perform various tasks on it.
      singulars
    2. CO@alex: Thanks for the response. Fair point - it doesn’t seem unreasonable to give a tyre shop my whole car to replace the tyres. But what if I had a deeper object hierarchy as follows… `tyreShop.ReplaceTyres(myFamily.Wife.Car, every2Years)`. Giving the TyreShop a reference to myFamily definitely doesn't feel right. But there is a chance that I might do `myFamily.Wife = new Person() { Car = new VWGolf(); }` if I catch her cheating with the milk man. In this case, the instance of Car that was given to the TyreShop needs to be updated to reflect the car of my new wife.
      singulars
    3. COI note that I have been somewhat contorting my example in response to people’s suggestions, and I apologize for doing this. I’ve found it a little difficult to capture the true complexities of my domain in a simple example. I will wait some time and then accept the response that best addresses the actual question/example I posed.
      singulars
 

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