The String or the Cat: A New .NET Framework Library
For years applications have been built that accept user input. Most user input starts out as a string. Strings are a universal representation of arbitrary data coming into a computer. However, most data does not remain as a string for very long. User input often ends up getting parsed or converted into another data type, such as an integer, Boolean value, or a date.
The concepts presented here are based on a thought experiment proposed by scientist Erwin Schrödinger. While an understanding of quantum physics will help to understand the new types and APIs, it is not required.
Mutation of data types leaves a software developer in an interesting situation if the data was not formatted properly and thus could not be mutated. For example, the string “abc123” cannot be parsed into an integer. Many frameworks deal with this situation by immediately reporting an error condition. That error could be an exception that was thrown or a Boolean value of false to indicate failure. Keeping track of these states can easily introduce uncertainty into an application.
StringOr<TOther>
The first class being introduced is the StringOr class. It is a generic class that encapsulates both the original string user input as well as the result of an attempted conversion operation.
namespace System.QuantumEntanglement { public class StringOr<TOther> { public StringOr(string stringValue, TOther otherValue); public string StringValue { get; } public TOther OtherValue { get; } } }
The usage of the StringOr class is quite simple. An API that wishes to return data that might not have been parsed successfully can use the StringOr class to represent the dual state of its return value.
For example, a typical application might have a TextBox for entering an integer quantity:
If the consumer of this API wants the original value, they can use the StringValue property. Otherwise they can use the OtherValue property, which in this case is the successfully parsed integer.
The following diagram illustrates the dichotomy with a scenario familiar to many physicists:
SchrodingOr<TDead, TAlive>
The second class being introduced is the SchrodingOr class. It is similar to the StringOr, but with a distinct difference regarding its states. The SchrodingOr can represent any two data types: It is not restricted to one of the types being a string.
namespace System.QuantumEntanglement { public class SchrodingOr<TDead, TAlive> { public SchrodingOr(TDead dead, TAlive alive); public TAlive Alive { get; } public TDead Dead { get; } } }
The usage of the SchrodingOr class is also similar to the StringOr. An API that wishes to return data that might be either of two data types, but never both data types at the same time, can return a SchrodingOr. The following code sample demonstrates a case where an API returns a value that can be either a bool or a DateTime.
Depending on the effects of quantum mechanics one of the two properties will return its value, while the other will throw a HeisenburgException. As before, the following diagram illustrates the dichotomy of the two possible values:
System.QuantumEntanglement Community Technology Preview
While the new .NET Framework library is still in relatively early stages of development, the team has decided to release a sneak preview to get feedback from our users. The link below contains the full source code for the new library, as well as unit tests that demonstrate proper usage of the libraries.
- Class library with unit tests (requires Visual Studio 2008 Professional and higher)
Please send all questions, comments, and suggestions to string.or@microsoft.com.
Many thanks,
The .NET Framework Quantum Mechanics Team