Microsoft is really taking major strides in terms of improving the programming experience for developers on all current and future computing platforms. That's why, earlier today, Microsoft released a beta of the new "System.QuantumEntanglement" library. This library, being considered for integration with .Net 4.1 (which, my sources say, is codenamed "Looflirpa"�), provides features for developers working on quantum computers. I won't go in to too much detail, but this essentially means working in an environment where objects can be in many different states (possibly an infinite number) and where observation may change the state of an object. Microsoft has proposed a few options, but I have some additional ideas.
Microsoft's Proposal
Eilon Lipton, of the ASP.Net MVC team, is proposing a number of new additions to the .Net BCL in 4.1. The first, is a class called "StringOr<T>" which has the following API:
namespace System.QuantumEntanglement {
public class StringOr<TOther> {
public StringOr(string stringValue, TOther otherValue);
public string StringValue { get; }
public TOther OtherValue { get; }
}
}
This class is used to encapsulate a value which may be a string, but may be another value. This is a common user-input scenario, since almost all user input arrives as strings, but will usually be converted to another data type. He also proposes a more general class called "SchrodingOr<TDead, TAlive>"�
namespace System.QuantumEntanglement {
public class SchrodingOr<TDead, TAlive> {
public SchrodingOr(TDead dead, TAlive alive);
public TAlive Alive { get; }
public TDead Dead { get; }
}
}
This generalizes StringOr<T> to support any two types.
Eilon's not the only one talking about this, Scott Hanselman (well known Microsoft blogger), and Rob Conery (Author of the .Net Object-Relational Mapper: SubSonic) have also posted on this topic. So I figured I'd add my comments to the blogosphere.
StringOr and SchrodingOr are great starts, but what if we need to represent objects which may be in 2 states? What about 3 states? Infinite states? That's where my proposals come in. I propose the following additions to Eilon's library:
Generalizing SchrodingOr
The first is SchrodingOr<T1, T2, T3, T4, T5, ... , Tn> (for infinite n). This allows the developer to represent objects in as many states as they want.
C# 5.0 Compiler Support
I'm also proposing the following language syntax to help developers work with these types. Similar to the way the C# compiler converts "int?" to "Nullable<int>" (which would now be replaced with "SchrodingOr<int, Void>" as it more accurately represents the concept of a type which may or may not have a value), the new syntax takes the following:
int?string?bool?DateTime? foo = GetUserInput(...);
And produces output code which looks like this:
SchrodingOr<int, DateTime, bool, string> foo = GetUserInput(...);
Then, we can defer observation until a later time, using the AsA<T>/AsAn<T> method (since Eilon's Alive and Dead properties no longer work in an infinite state environment). If we just want to observe the type and then make a decision, we can use IsA<T>/IsAn<T> methods (of course, both are required, just in case the type starts with a vowel), which returns a boolean indicating if the object is of the type T. For example:
if(foo.IsA<string>()) { return foo.AsA<string;>; }
Probabilistic Observation
Sometimes, we may want to observe an object only if there is a high probability of it being in the right state, to avoid additional quantum variations. To do this, we can use the CouldBeA<T>/CouldBeA<T> methods. These can, optionally, accept a probability threshold beyond which a object is considered in the right state.
Quantum Snapshots
Unfortunately, in between a call to IsA/An/CouldBeA/An and AsA/An, the state of the object may change. So, we may wish to observe an object and record the state of the object at the time of observation. Since in observing the object we may change its state, our observations must be recorded in "Snapshots"�. The snapshot is an instance of QuantumSnapshotOf<TSchrodingOr> (where TSchrodingOr must be one of the SchrodingOr<T1, ..., Tn> types), and has a property "Taken", which is a DateTimeOffset (after all, we need precise timing here) containing the exact time that the snapshot was taken.
Here's an example combining Probabilistic Observation and Snapshots:
if(foo.CouldBeA<string>()) {
QuantumSnapshotOf<SchrodingOr<int, string>> snapshot = foo.Observe();
if(snapshot.Taken.Day == 1 && snapshot.Taken.Month == 4) {
throw new UnreliableObservationException(snapshot, "The data is unreliable");
}
return snapshot.AsA<string>();
}
Summary
I think this is really interesting, forward-thinking, stuff from Microsoft. Eilon has done some fantastic work getting the ball rolling, and its up to us to give our feedback! Microsoft has created a section on MSDN for discussion on these new features: http://msdn.microsoft.com/en-US/QuantumEntanglement/Default.aspx