In the many core world we now live in, programmers have begun to pay more attention to threading, immutability and functional style programming. For those of you keen to these trends, you may have heard of futures or promises. These constructs act as proxies for values that are yet to be calculated and are typically used to aid with synchronization issues in concurrent programming languages, such as C#.
Similar to futures, pasts, are a useful and almost unheard of construct that helps developers maintain previously calculated values. Pasts are not a C# language feature, but they are easy to develop on your own. The idea is simple:
- Calculate a value, v.
- Create a new Past object P holding the value v.
- Use the Past object P when you need the value v.
As you can see, this straightforward alternative to referencing variables directly can really be useful in programs which need to access the same variable value multiple times. Let's see what a Past object might look like.
class Past<T>
{
private T value;
public T Value { get { return value; } }
public Past(T Value)
{
value = Value;
}
}
We've essentially created a heap dwelling wrapper for some data, potentially primitive stack dwelling data. Yes I know what you're thinking, and the answer is a resounding Yes! Pasts can box primitive data better than that kludgey old object thing can! And you don't have to deal with inheritance chains and casting, yuck! Pasts are almost as easy to use as they are to develop:
int iNeedToUseThisAlot = 10;
Past<int> Handy = new Past<int>(iNeedToUseThisAlot);
Console.WriteLine(Handy.Value.ToString());
The re-usability in the Past class above should sell itself! Embrace and use Pasts, they are a truly unique and useless useful data structure. Stop back the same time next year for an eye opening post on Durings, you'll actually see the bits themselves change!
Looks like I'll be the first person to admit I fell for it, but only until I read "...can really be useful in programs which need to access the same variable value multiple times."
Posted by: Allan | April 01, 2009 at 05:09 PM