Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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?



I suspect the second method is just an example of how you could make an asynchronous operation that performs a subtracting of two numbers, after a method (CountEveryBitByHand) is completed.

To keep it simple they should have left out the second method.

Unless I'm incredibly wrong!


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.


(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....


Miguel is a pretty l33t developer, so he glosses over a lot of the details.

I suspect that code is contained in a class library project. Aka, generates a DLL. So no, not all sealed classes will be components.

The second method is there to illustrate what asynchronous programming will look like in C# 5. Native support for async is THE big feature in C# 5. The "await" allows you to call slow blocking code without a thorny nest of callbacks. There's some magic behind await. You can find out more on Google. Miguel could've definitely left out the SubAsync method in this example for clarity.

A component that adds 2 means a DLL that exposes the methods Add and SubAsync. In WinRT, you can code libraries in C# and use them in C++. Or Javascript.


Aha, that almost makes sense. You're saying "add 2" might mean a component that exposes two methods. Not sure what exactly he is demonstrating there, actually. I mean, here's a DLL written in C that exposes the same two methods (although not asynchronous):

  int Add(int a, int b) { return a + b; }
  int Sub(int a, int b) { return a - CountEveryBitByHand(b); }
So, I guess the example is just to show the async stuff, really.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: