
WinRT demystified - friism
http://tirania.org/blog/archive/2011/Sep-15.html
======
krig
_The following is the full source code for a component that adds 2:_

    
    
      public sealed class AddTwo {
        public int Add (int a, int b)
        {
           return a + b;
        }
      
        public async IAsyncOperation SubAsync (int a, int b)
        {
           return a - await (CountEveryBitByHand (b));
        }
      }
    

Sorry for being an idiot here, but.. does this example make any sense to
anyone else? There's a method that adds two numbers, but the async operation
is SubAsync which subtracts something passed through CountEveryBitByHand from
a... passed through something called await? Is this obvious to other people?
Does he mean that any sealed class automatically becomes a component, so the
AddTwo part is just the Add method? What does "a component that adds 2" mean
anyway?

Is it a joke and I'm not getting it?

~~~
joeshaw
CountEveryBitByHand() is a function which returns a promise. (I don't know if
that's what they call it in C# 5.)

The await keyword is a new one in C# 5 which halts execution until the promise
is fulfilled. Then execution is resumed from that point, and the expression
can be evaluated and the function returns.

It's similar to how inlineCallbacks in Twisted work, if you're familiar with
those.

~~~
davidpadbury
_(I don't know if that's what they call it in C# 5.)_

Tasks - [http://msdn.microsoft.com/en-
us/library/system.threading.tas...](http://msdn.microsoft.com/en-
us/library/system.threading.tasks.task.aspx).

------
d4nt

      When you use C# and VB, you are using the full .NET 
      framework. But they have chosen to expose a smaller 
      subset of the API to developers to push the new vision 
      for Windows 8.
    

For me, this is a really interesting point that this article doesn't fully
get. When you use WinRT you might be using the C# compiler, CLR runtime and
.NET string & collection classes, but there are huge sections of the .NET BCL
that now look like a lame duck. WinRT is the new way to do XML, JSON, HTTP,
networking, security, globalisation, threading, printing... (more here:
[http://msdn.microsoft.com/en-
us/library/windows/apps/br21137...](http://msdn.microsoft.com/en-
us/library/windows/apps/br211377\(v=VS.85\).aspx))

You can still use the full .NET class library when doing ASP.NET, but who
wants to use different JSON classes for App development vs. Web development?
Won't the WinRT version gradually become the preferred way of doing JSON in
both environments, with the .NET BCL existing to fill in the server side only
gaps?

~~~
smhinsey
This is a really interesting question. I did some quick skimming over the docs
there and even where there is conceptual overlap, the BCL wins in terms of
feature breadth. It would be challenging to replace it, I think.

My suspicion is that .NET 5 will merge the two.

------
batina
Great explanation. Can't wait to finish downloading Windows 8 to start playing
with it.

Asynchronous API that will really make a difference for the end user, right?
There won't be any more waiting for UI to unfreeze :)

~~~
IvoDankolov
Not just for the user - calling asynchronous tasks using the new C# or VB
keywords is so mind-bogglingly easy that you'll just have no excuse not to do
it when writing apps.

Even the use cases that are not covered by the two keywords, like fetching a
whole bunch of stuff together or issuing a lot of requests and having the data
trickle in as they are completed one by one are handled easily with the Task
API, to the point where you have WhenAny and WhenAll methods and you just do
"await Task.WhenAll(tasks)".

------
josteink
Completely unrelated, but if the article didn't overwhelm you, the linked
video session really is worth checking out.

<http://channel9.msdn.com/Events/BUILD/BUILD2011/BPS-1004>

It shows just how much attention to detail Microsoft has put into the UX
design of Metro and Windows 8. Can't wait to see the finished product.

~~~
IvoDankolov
Aye, as someone who didn't have many expectations (though I didn't expect
Windows 8 to be a failure either, I'm not one to rage about changing the
sacred desktop look), throughout the keynote and this video they managed to
pleasantly surprise me many times.

The design aside (which is an entire topic on its own), some of the
functionality decisions like contracts just make you wonder why the sodding
hell didn't we have these sooner. I mean there's the half-baked drag and drop
handler than no one actually bothers to implement even when it makes absolute
sense to do so, and that's it. Being able to pull things out of a web service
from the file picker, on the other hand, just makes me go "I want this
yesterday."

------
chucknthem
"You might be thinking that you can use some trick (referencing the GAC
library instead of the compiler reference or using reflection to get to
private APIs, or P/Invoking into Win32). But all of those uses will be caught
by AppStore review application"

I'm curious to know, how will the review application be able to catch your use
of the private API through reflection? It seems like a pretty hard problem
that could be a big security risk if done incorrectly.

~~~
beagle3
I don't know, but it just _might_ be time for Microsoft to do jail-style
security, in which you can't actually reflect from libraries you have no
permission to.

------
rwolf

        and Javascript uses promises and "then ()".
    

What is "then ()"? Have they created their own wacky fork of Javascript?
Javascript already handles asynchronous programming (feel free to argue that
is doesn't do it "right" or even "well"), so why extend the language?

~~~
kevingadd
It's a method with a signature like this:

    
    
        promise.then(callback)
    

so:

    
    
        var p = doSomethingAsync();
        p.then(function () { alert("hello"); })

~~~
nxn
I'm assuming they only chose to use this pattern for the purpose of keeping
function/method signatures as uniform across the languages as possible? I
can't see another advantage to this in JS. Seems like all it's doing is
wrapping the call to the actual async operation within the 'then' function,
and then probably using the callback as the last argument or storing it
somewhere where it will be accessible to the async operation right before it
returns.

------
beagle3
Does anyone know if it will have a Sound API? .NET still doesn't. Neither does
it have usable Direct{3D,Sound,X} binding.

~~~
jbk
Use WASAPI: [http://msdn.microsoft.com/en-
us/library/dd371455%28v=vs.85%2...](http://msdn.microsoft.com/en-
us/library/dd371455%28v=vs.85%29.aspx)

~~~
beagle3
Google "wasapi c# example", go on, google it.

------
HardyLeung
Wow, very well written. Thanks Miguel!

------
kbd
It's amazing to see just how much Apple is driving the industry. Microsoft is
making an app store, completely rethought their UI and programming model for
mobile devices...

~~~
JoeAltmaier
Um, it sounds like 'mobile' is driving the industry. Apple is a player too,
just like everybody else.

~~~
shimonamit
Don't forget that Apple invented the new 'mobile' as we now know it -
hardware, UX, app store etc. Everybody else is following.

~~~
cryptoz
Apple is also following. See iOS5 notifications copying Android notifications
as an example.

