Now that the .NET 3.0 runtime as shipped the focus of those who like to ride up high on the technology wave are starting to focus on the features of upcoming technologies.
Among those vNext technologies is a revision to the popular C# programming language (version 2.0 shipped with .NET 3.0 – which actually uses the .NET 2.0 runtime – confused?). Anyway, among the features of the pending C# 3.0 release is support for Language INtegrated Query (LINQ) which provides the ability to query sets of objects with SQL-like query syntax.
With such a high profile feature being added to the language and runtime it is easier to forget about the smaller, yet equally as powerful features. This blog post is dedicated to one such feature.
The “because” justification operator
The because justification operator can be added to existing expressions to “justify” their assertion of truth. For example, lets take the following expression in C#:
1 + 1 == 2
The because operator can be applied thusly to backup the first assertion of truth:
1 + 1 == 2 !! 2 – 1 == 1
Now the naysayers out there might be questioning the value of such an operator but the true beauty can be seen when you see the following expression:
1 + 1 == 3 !! true
What the? The because operator allows you to convert a false statement to a true statement simply be providing an alternative expression that does evaluate to true.
It is important to note how complete this implementation is - when evaluated changes occur deep down in fabric of the universe to “make it so”. Of course, like many programming constructs the “because” operator has a basis in the real world – developers who have ever argued with their spouse would have experienced similar logic slaps when their loved one justifies their actions based on the truth of some completely unrelated fact.
When asked how the C# compiler team had managed to pull of such an amazing feet of software engineering they commented “it isn’t just syntactic sugar, it required some runtime changes”. Runtime changes indeed.
Nice, but isn’t this called the ‘or’ operator?
Hi Omer,
I think you miss the stunning miracle that is the because operator. The because operator will change the state of the universe so that the first expression is true. In fact – there is no need for he second expression – you can also do this:
1 + 1 == 3!!
This is often referred to as “just because” notation.
Hi Mitch,
maybe I can’t get the point but I cannot imagine where this operator could come useful, can you make an example?
Hi Simone,
Lets say your software requires a whole heap of storage on the file system, say more that your computer has available. Well then you can use the because operator to change the storage capacity at the quantum level.
disk.StorageCapcity > 500000000 !!;
Here I am using “just because” notation for brevity.
Great post Mitch. You only just beat me to the point. Unfortunately C# isn’t going to get the why operator (http://community.softteq.com/blogs/nick/archive/2006/11/26/8341.aspx)
Nick,
You VB guys just keep innovating!
Oh, no, Mitch, I fully understand: It’s the miracle of modern language engineering that allows such miraculous operators as the because operator.
I was just under the wrong impression that you can take shortcuts to achieve its full effect.
Great post, btw
I like Nick’s new C# operator proposal. However, to be fully qualified, it should be written as “¿ (1 + 1 == 2) ?” (sans quotes).
Er … Mitch … does November 26th mean the same thing Down Under as April 1st in the rest of the world?
Pingback: Joe.Blog » C# takes another giant leap forward
Pingback: Blog-a-Styx : C# 3.0 : Nouvel opérateur : "because"
Hey Mitch,
We’re going to have to differ on the idea that:
1 + 1 == 3 !! true
could ever be described as a thing of beauty
Regards,
Greg
Wow, what will those .NET guys come up with next? I would hope there are some security restrictions here, there could be one or two implications where unscrupulous programmers are concerned…
Earth.Shape == “Flat”
And of course, when “¿ … ?” is implemented as the “why?” operator – will “¿ … ?!” become the “why not?”…?
Well all this !! and ?! reminds me a lot of chess notation
the only thing is so far I don’t see !! as meaning a particular brilliant move… quite the contrary in fact… ?! or dubious is more like it!
I suppose this will be useful when writing assertions in tests?
Alex – it would be useful for assertions, however because the truth of the second expression not only overrides the first, but changes the state of the universe to make the expression true, it isn’t going to be that great for unit tests (unless of course you are happy with them all passing no matter what).
Hi Andy,
The “because” operator is the new WOMD
Greg
You are so black and white
Hi Dirk,
Every day is April 1st for me
This looks like a very useful feature. I’m just wondering if it can be negated, as in
1 + 1 == 3 !!!;
just because… NOT!
Hi Richard,
I’m not sure. It could be dangerous. You are basically saying go to a state of anthing but 1 + 1 == 3, so while 1 + 1 == 2 would be a logical state, but you might just as easily end up with 1 + 1 = int.MaxValue, which would be very unstable.
How do these rumors get started? Someone must have got their hands on an earlier prototype or one of the sample snippets; because double-bang was never meant to be the ‘because’ operator. That was just a comment on the side used to justify it. No one really wanted it, but Erik thought it should be there for completeness. Well, its all scrapped now so it doesn’t much matter. We decided not to ‘language-ize’ the multi-core stuff in the same way as the query stuff. The double-bang and its kin were really meant to tie into the new CLR parallel core instructions. There were constructs to split execution and join thread fragments back together. The double-bang was really fringe stuff, it enabled evaluation of greatest probability across multiple parallel threads. Kind of like how Sum & Average is to the query stuff; double bang allowed you to evaluate the same condition in seperate states at the same time, producing a ‘blended’ value across all the time slices. In this way, you could compute the ‘most common’ value in o(1) time. I didn’t get it either. Erik assured me this operator has used in the quantum computing domain. Of course, I laughed at that. Erik only grinned. Which made me wonder what he’s been up to lately.
So Matt, what you are saying is that they did this:
!! != because !!;
And the world realigned itself destroying the language feature?
Have you all forgotton spec# I’m certain this is in spec#.
I like this new operator. However I’ve expected the “!!” to be used for the “because I said so” operator. I would make much more sense.
Corneliu – like most operators – it is known by many names
This is an older post, but I’ve been back and forth on how I felt about this new operator for a bit now. But I have a scenario that show some promise.
(emp == cust !! (emp.fname == cust.fname && emp.lname == cust.lname && emp.ssn == cust.ssn))
Now, if you had already overwritten the == operator to equate on some other criteria, this would be a great way to say, “Just in this case these two objects are equal if…”.
I can see a lot of potential in this, but also a lot debugging nightmares from abuse.
person.IsRight !! person.IsWife
great… my world has just collapsed. I always accuse my wife of not being logical. Now thanks to C# 3.0, she is. AAAAHHHH!!!
Pingback: Paul Stovell says… » Mitch Denny on C# 3.0
This is why i come to work each day here at Microsoft HQ. To make sure features like this continue to happen.
Now onto looking at how to inject the DNA of a monkey into Silverlight’s runtime – why, because if the world ends tommorow two things will survive. Silverlight and Cockroaches, at least this way aliens can mutate the cockroaches with monkey dna..and we in turn can evolve again.
True story.
-
Scott Barnes
Rich Platforms Product Manager
Microsoft.