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.

clip_image002

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:

clip_image004

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:

clip_image006

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.

clip_image008

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:

clip_image010

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.

Please send all questions, comments, and suggestions to string.or@microsoft.com.

Many thanks,

The .NET Framework Quantum Mechanics Team

Published Wednesday, April 01, 2009 3:00 AM by Eilon
Filed under: , ,

Comments

# String Input Handling + Quantum Mechanics! at Lost In Tangent

Pingback from  String Input Handling + Quantum Mechanics! at  Lost In Tangent

# .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

Wednesday, April 01, 2009 3:50 AM by Scott Hanselman's Computer Zen

# Scott Hanselman's Computer Zen - .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

Pingback from  Scott Hanselman's Computer Zen - .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

# .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

Wednesday, April 01, 2009 3:50 AM by Scott Hanselman's Computer Zen

# .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

Wednesday, April 01, 2009 3:51 AM by ASPInsiders

As web programmers, we use a lot of strings to move data around the web. Often we’ll use a string to

# .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

Wednesday, April 01, 2009 3:53 AM by Microsoft Weblogs

As web programmers, we use a lot of strings to move data around the web. Often we’ll use a string to

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 4:07 AM by jinfu

1st April, thanks

# .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

Wednesday, April 01, 2009 4:26 AM by Scott Hanselman

As web programmers, we use a lot of strings to move data around the web. Often we’ll use a string to

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 5:00 AM by Steven Sanderson

Very interesting stuff! I can see a lot of practical applications of this. For example, in ASP.NET MVC, incoming data goes through the model binder, which wouldn't necessarily know what type the input data was supposed to represent (or even what culture settings should be used to parse the string value) until it is later "observed" by an action method and its type/culture becomes fixed.

Would it be possible to use this technology to replace WCF/.NET remoting too? Assuming that you can put two objects into a "quantum entangled" state, you could then transmit one of them across the wire, keeping the other locally. Later, any changes made to your local object would instantly cause effects on the remote object, no matter how far away it now is, and even if no network connectivity was available. This would be much faster than any of the current forms of RPC, and wouldn't require the objects to be serializable.

Can't wait for .NET 4.1, it's going to be awesome!

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 5:05 AM by nishanthnair

Awesome..... A change from orthodox programming will be well received.

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 5:29 AM by Pop Catalin

I love pink ponies and unicorns day :) ...

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 5:36 AM by Andrew Nurse

Very cool stuff Eilon.  Though I think it needs to be generalized a little further than two states.  I've posted a proposal for that on my blog (see shameless plug below):

blog.vibrantcode.com/.../QuantumComputingInNet.aspx

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 5:40 AM by ponnu

Interesting stuff.

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 6:24 AM by Veggerby

Good one :)

"Please send all questions, comments, and suggestions to string.or@microsoft.com." -->

mailto:aprilfools@example.com

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 6:24 AM by Erwin Schrödinger

You should pay me in case you release such a framework or I will sue Microsoft because of copyright infringement.

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 6:26 AM by Leonid Shirmanov

Looks promising, but there is no actual type conversion for now. Will it be in BCL itself?

# April Fools Day roundup | TechRepeater: Microsoft, WordPress & Social Networking

Pingback from  April Fools Day roundup | TechRepeater: Microsoft, WordPress & Social Networking

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 7:26 AM by cubanx

Kudos to you and Scott for making this better by putting it out on April 1st ;)

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 8:00 AM by Burak SARICA

Nice April 1 Joke "The .NET Framework Quantum Mechanics Team" :)

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 8:43 AM by Daniel Teixeira

Nice one! but HeisenburgException ought to be HeisenbergException.

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 9:20 AM by whichman

This is BS. This is the test you want to perform:

StringOr<int> userInput = "abc123";

Assert.AreEqual(123, userInput.OtherValue);

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 9:21 AM by Patrik

April fools!

# April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I.

Pingback from  April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I.

# 未来就在眼前

Wednesday, April 01, 2009 9:53 AM by DiryBoy

今天真是大日子!许多看似在未来才会有的技术都提前浮出水面了! 1、Google 最新浏览器,支持将普通网页 3D 化,你只要戴上 3D 眼镜,就能体验 3D 互联网! 展示地址:

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 10:50 AM by findleyd

I'm anxiously awaiting the release of the QLR! :)

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 10:54 AM by Greg

I'd discourage this cute code in production systems.  It would not pass our code review/code quality/code security requirements.

StringOr class fails the requirement that well engineered systems do not delay exception handling, ignore errors until later or allow an error to be resurrected multiple times (i.e., be each object that calls StringOr to get an integer when StringOr is not an integer "123abc").

# Type Safe Variants the Erwin Schr&ouml;dinger way

Wednesday, April 01, 2009 11:05 AM by Type Safe Variants the Erwin Schrödinger way

Pingback from  Type Safe Variants the Erwin Schr&ouml;dinger way

# Joshua Schaible&#8217;s Blog &raquo; Blog Archive &raquo; Full List Of Online April Fools Jokes [Everything Geeky]

Pingback from  Joshua Schaible&#8217;s Blog  &raquo; Blog Archive   &raquo; Full List Of Online April Fools Jokes [Everything Geeky]

# Joshua Schaible&#8217;s Blog &raquo; Blog Archive &raquo; Full List Of Online April Fools Jokes [Everything Geeky]

Pingback from  Joshua Schaible&#8217;s Blog  &raquo; Blog Archive   &raquo; Full List Of Online April Fools Jokes [Everything Geeky]

# Saintless &raquo; Fools of April on the Web

Wednesday, April 01, 2009 11:55 AM by Saintless » Fools of April on the Web

Pingback from  Saintless   &raquo; Fools of April on the Web

# April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I.

Wednesday, April 01, 2009 12:02 PM by Aggregator @ Bitubique

Wow. April Fools Is In full swing. The Guardian goes all Twitter, ditching the printed version and the website. &#8220;Experts say any story can be told in 140 characters.&#8221; Amazon launches a brand new cloud computing service called Floating Amazo

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 12:32 PM by Dimitris Foukas

Eilon,

The solution you describe can be easily handled with a generic facility for Tuples - among other things this will help aligning the BCL with F# as a first class citizen! It will also help reduce the semantic load of output variables.

If my petition is not successful then  I would ask to modify the definition of StringValue to return the remainder of the string after partially parsing the integer value, i.e.

if userInput = "123inches"

then userInput.OtherValue -> 123

and userInput.StringValue -> "inches"

You have to decide on the representation of complete parsing failure.

This will allow us to do much more elaborate input parsing by combining simple parsers (parameterized by the type of OtherValue) using Linq techniques to emulate monadic parsers.

Just google for Parsec.

Regards,

Dimitris Foukas

# April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | New Web 2.0 Magazine

Pingback from  April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | New Web 2.0 Magazine

# April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | New Web 2.0 Magazine

Pingback from  April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | New Web 2.0 Magazine

# April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | thepagerank

Pingback from  April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | thepagerank

# April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | Recopylator

Pingback from  April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | Recopylator

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 6:21 PM by Ryan

What would the interface look like for DoppelgangOr<T>?

# re: The String or the Cat: A New .NET Framework Library

Wednesday, April 01, 2009 7:08 PM by Lee Brandt

@Erwin S. Let it go man, the cat is dead... or is it? Happy 1st

# Quantum computing done right

Wednesday, April 01, 2009 8:01 PM by Community Blogs

I know as a good microsoftee I should be supportive of what my employer does no matter what it is, and

# re: The String or the Cat: A New .NET Framework Library

Thursday, April 02, 2009 2:26 AM by saraf uddin

i like both the class.. and its very clear that i will be using this a lot in my future projects. But what's with the HisenburgException. Does it have to do anything with "Hisenburg's Uncertainity Principle" for location of an electron ?

# re: The String or the Cat: A New .NET Framework Library

Thursday, April 02, 2009 5:35 PM by Ion Singh

Add the following code to do a implicit conversion.

StringOr<int> age= "5";

int age2 = age;

public static implicit operator StringOr<TOther>(string value)

       {

           TypeConverter tc = TypeDescriptor.GetConverter(typeof(TOther));

           TOther result = (TOther)tc.ConvertFrom(value);

           return new StringOr<TOther>(value, result);

       }

Also, you can write an generic String.Parse extension method, to do the same thing.

www.jigar.net/.../viewhtmlcontent319.aspx

# re: The String or the Cat: A New .NET Framework Library

Thursday, April 02, 2009 6:09 PM by Jerome

Anyone who took this seriously, despite preceding references to April Fool's in the comments, should think about a career change to turnip picking :)

# The String or the Cat: A New .NET Framework Library - Eilon Lipton's B

Thursday, April 02, 2009 10:14 PM by Web Development Community

You are voted (great) - Trackback from Web Development Community

# re: The String or the Cat: A New .NET Framework Library

Sunday, April 05, 2009 5:40 PM by Coder

I dont understand the new feature. What the difference? What the advantage over, for example, int.Parse the StringOr<int> has? In failure case, what the StringOr behavior?

A simple and practical example (a comparative piece of code also, please)?

# .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

Pingback from  .NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

# Better String Input Handling - TechLog &laquo;Blog Of Priyo

Monday, April 20, 2009 4:04 AM by Better String Input Handling - TechLog «Blog Of Priyo

Pingback from  Better String Input Handling - TechLog &laquo;Blog Of Priyo

# re: The String or the Cat: A New .NET Framework Library

Wednesday, May 20, 2009 1:58 AM by MisterFantastic

Hi,

Can you please tell some practical usage of these functions ?

Thanks,

Thani

# Top-silverlight &raquo; Blog Archive &raquo; Silverlight 3D Overview

Pingback from  Top-silverlight  &raquo; Blog Archive   &raquo; Silverlight 3D Overview

# April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | Reviews

Pingback from  April Fools: YouTube Flails, Amazon Cloud Computing In A Blimp, 3D Chrome Browsing, Google Masters A.I. | Reviews

# Thr3e, 2007

Friday, December 25, 2009 1:32 PM by Thr3e, 2007

Pingback from  Thr3e, 2007

# re: The String or the Cat: A New .NET Framework Library

Tuesday, April 26, 2011 8:15 PM by Martin

Four negatives in an extension method makes me antsy.

# Cool Extension Methods for New StringOr | Rob Conery

Thursday, November 10, 2011 6:16 PM by Cool Extension Methods for New StringOr | Rob Conery

Pingback from  Cool Extension Methods for New StringOr  |  Rob Conery

Leave a Comment

(required) 
(required) 
(optional)
(required)