<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Generalities &amp; Details: Adventures in the High-tech Underbelly</title>
    <link>http://www.bluebytesoftware.com/blog/</link>
    <description>Joe Duffy's Weblog</description>
    <language>en-us</language>
    <copyright>Joe Duffy</copyright>
    <lastBuildDate>Mon, 12 Jul 2010 03:18:30 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.8.5223.2</generator>
    <managingEditor>joe@bluebytesoftware.com</managingEditor>
    <webMaster>joe@bluebytesoftware.com</webMaster>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=7a57f623-d65d-4212-973d-29bdcf61dd3a</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,7a57f623-d65d-4212-973d-29bdcf61dd3a.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,7a57f623-d65d-4212-973d-29bdcf61dd3a.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=7a57f623-d65d-4212-973d-29bdcf61dd3a</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      That immutability facilitates increased degrees of concurrency is an oft-cited dictum.
      But is it true? And either way, why?
   </p>
        <p>
      My view on this matter may be a controversial one. Immutability is an important foundational
      tool in the toolkit for building concurrent – in addition to reliable and predictable
      – software. But it is not the only one that matters. Making all your data immutable
      isn’t going to instantly lead to a massively scalable program. Natural isolation is
      also critically important, perhaps more so. And, as it turns out, sometimes mutability
      is just what the doctor ordered, as with large-scale data parallelism.
   </p>
        <p>
        </p>
        <h1>Isolation first; immutability second; synchronization last
   </h1>
        <p>
      Stepping back for a moment, the recipe for concurrency is rather simple. Say you’ve
      got multiple concurrent pieces of work running simultaneously (or have a goal of getting
      there); for discussion’s sake, call them <em>tasks</em>. Take two tasks. The first
      critical decision has two cases: either these tasks concurrently access overlapping
      data in shared-memory, or they do not. If they do not, they are <em>isolated</em>,
      and no precautions associated with racing memory updates are needed. If they do share
      data, on the other hand, then something else must give. If all concurrently accessed
      data is <em>immutable</em>, or all functions used to interact with data are <em>pure</em>,
      then dangerous concurrency hazards are avoided. All is well. If some data is mutable,
      however, then this is where things get tricky, and higher-level <em>synchronization</em> is
      needed to make accesses safe. This decision tree is straightforward and clear.
   </p>
        <p>
      I have listed those four attributes – isolated, immutable and pure, and synchronization
      – in a very intentional order. Thankfully, this order mirrors the natural top-down
      hierarchical architecture of most modern object- and component-based programs: we
      have large containers that communicate through well-defined interfaces, each comprised
      of layers of such containers, and somewhere towards the leaves, a fair amount of intimate
      commingling of knowledge regarding data and invariants.
   </p>
        <p>
      This order also reflects the order of complexity and execution-time costs, from least
      to most. Isolation is simple, because components depend on each other in loosely-coupled
      ways, and in fact scales superiorly in a concurrent program because no synchronization
      is necessary, the “right” data structure may be chosen for the job – immutable or
      not – and locality is part-and-parcel to the architecture. Immutability at least avoids
      the morass of synchronization, which can affect programs immensely in complexity,
      runtime overheads, and write-contention for shared data. It is clear that synchronization
      is something to avoid at all costs, particularly anything done in an ad-hoc manner
      like locks.
   </p>
        <p>
        </p>
        <h1>Making the concurrency
   </h1>
        <p>
      But where did all this concurrency come from, anyway?
   </p>
        <p>
      It came from two things:
   </p>
        <ol>
          <li>
         The coarse-grained breakdown of a program into isolated pieces. 
      </li>
          <li>
         The fine-grained data parallelism. 
      </li>
        </ol>
        <p>
      On #1: Program fragments that are isolated are already half-way down the road to running
      concurrently as tasks. The second half of this journey, of course, is teaching them
      to interact with one another asynchronously, most frequently through message-passing
      or by sticking them into a pipeline. The details of course depend on what programming
      language you are using. It may be through agents, actors, active objects, COM objects,
      EJBs, CCR receivers, web-services, something ad-hoc built with .NET tasks, or some
      other reification. Nevertheless the isolation is common to all these.
   </p>
        <p>
      On #2: Data parallelism, it turns out, often works best with mutable data structures.
      These structures must be partitionable, of course, so that tasks comprising the data
      parallel operations may operate with logically isolated chunks of this data safely,
      even if they are parts of the same physical data structure. So chunks of them are
      isolated even though they don’t appear to be. This is trivially achievable with many
      important parallel-friendly data structures like arrays, vectors, and matrices. Capturing
      this isolation in the type system is of course no small task, though region typing
      gets close (see UIUC’s Data Parallel Java).
   </p>
        <p>
      But you usually don’t want these structures to be immutable, because they can be modified
      in constant-time and space if they are their classic simple mutable forms. Programmers
      doing HPC-style data-parallelism a la FORTRAN, vectorization, and GPGPU know this
      quite well. Compare this a world where we are doing data-parallelism over immutable
      data structures, where modifications often necessitate allocations or more complicated
      big-oh times due to clever techniques meant to avoid such allocations, as with persistent
      immutable data structures. This is likely less ideal. It is true that some data parallel
      operations are not in-place against mutable data – as with PLINQ – at which point
      purity, but not immutability, is key. The two are related but not identical: immutability
      pervades the construction of data structures, whereas purity pervades the construction
      of functions. But if you can get by with one copy of the data, why not do it? Particularly
      since most datasets amenable to parallel speedups are quite large.
   </p>
        <h1>Immutability: the bricks, not the mortar
   </h1>
        <p>
      Notice that the concurrency did not actually come from immutable data structures in
      either case, however. So what are they good for?
   </p>
        <p>
      One obvious use, which has little to do with concurrency, is to enforce characteristics
      of particular data structures in a program. A translation lookup table may not have
      been meant to be written to except for initialization time, and using an immutable
      data structure is a wonderful way to enforce this intent.
   </p>
        <p>
      What about concurrency? Immutable data structures facilitate sharing data amongst
      otherwise isolated tasks in an efficient zero-copy manner. No synchronization necessary.
      This is the real payoff.
   </p>
        <p>
      For example, say we’ve got a document-editor and would like to launch a background
      task that does spellchecking in parallel. How will the spellchecker concurrently access
      the document, given that the user may continue editing it simultaneously? Likely we
      will use an immutable data structure to hold some interesting document state, such
      as storing text in a piece-table. OneNote, Visual Studio, and many other document-editors
      use this technique. This is zero-cost snapshot isolation.
   </p>
        <p>
      Not having immutability in this particular scenario is immensely painful. Isolation
      won’t work very well. You could model the document as a task, and require the spellchecker
      to interact with it using messages. Chattiness would be a concern. And, worse, the
      spellchecker’s messages may now interleave with other messages, like a user editing
      the document. Those kinds of message-passing races are non-trivial to deal with. Synchronization
      won’t work well either. Clearly we don’t want to lock the user out of editing his
      or her document just because spellchecking is occurring. Such a boneheaded design
      is what leads to spinning donuts, bleached-white screens, and “(Not Responding)” title
      bars. But clearly we don’t want to acquire a lock and then make a full copy of the
      entire document. Perhaps we’d try to copy just what is visible on the screen. This
      is a dangerous game to play.
   </p>
        <p>
      Immutability does not solve all of the problems in this scenario, however. Snapshots
      of any kind lead to a subtle issue that is familiar to those with experience doing
      multimaster, in which multiple parties have conflicting views on what “the” data ought
      to be, and in which these views must be reconciled.
   </p>
        <p>
      In this particular case, the spellchecker sends the results back to the task which
      spawned it, and presumably owns the document, when it has finished checking some portion
      of the document. Because the spellchecker was working with an immutable snapshot,
      however, its answer may now be out-of-date. We have turned the need to deal with message-level
      interleaving – as described above – into the need to deal with all of the messages
      that may have interleaved within a window of time. This is where multimaster techniques,
      such as diffing and merging come into play. Other techniques can be used, of course,
      like cancelling and ignoring out-of-date results. But it is clear something intentional
      must be done.
   </p>
        <p>
        </p>
        <h1>In conclusion
   </h1>
        <p>
      It is safe to say that immutability facilitates important concurrent architectures
      and algorithms. It can really help big time, for sure. But it is clearly no panacea.
      Whether mutability or immutability is the right choice for a particular data structure
      in your program, as with all things, depends.
   </p>
        <p>
      It could be the case that choosing a piece-table for storing your text facilitates
      large-scales of concurrency in version two of your software application, but that
      in version one you have no use for it. Making that call ahead of time may pay in spades
      down the road, even if it comes at a marginal cost up-front. Or it could be that choosing
      an immutable data structure costs you in time and space, and you never end up exploiting
      the fact that you could have shared that particular structure in a zero-cost way across
      agents in your program.
   </p>
        <p>
      One thing’s for sure: I’m glad to be programming in languages like C#, F#, Clojure,
      and Scala, where I’ve got a choice.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=7a57f623-d65d-4212-973d-29bdcf61dd3a" />
      </body>
      <title>Thoughts on immutability and concurrency</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,7a57f623-d65d-4212-973d-29bdcf61dd3a.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/07/12/ThoughtsOnImmutabilityAndConcurrency.aspx</link>
      <pubDate>Mon, 12 Jul 2010 03:18:30 GMT</pubDate>
      <description>&lt;p&gt;
   That immutability facilitates increased degrees of concurrency is an oft-cited dictum.
   But is it true? And either way, why?
&lt;/p&gt;
&lt;p&gt;
   My view on this matter may be a controversial one. Immutability is an important foundational
   tool in the toolkit for building concurrent – in addition to reliable and predictable
   – software. But it is not the only one that matters. Making all your data immutable
   isn’t going to instantly lead to a massively scalable program. Natural isolation is
   also critically important, perhaps more so. And, as it turns out, sometimes mutability
   is just what the doctor ordered, as with large-scale data parallelism.
&lt;/p&gt;
&lt;p&gt;
&lt;h1&gt;Isolation first; immutability second; synchronization last
&lt;/h1&gt;
&gt;
&lt;p&gt;
   Stepping back for a moment, the recipe for concurrency is rather simple. Say you’ve
   got multiple concurrent pieces of work running simultaneously (or have a goal of getting
   there); for discussion’s sake, call them &lt;em&gt;tasks&lt;/em&gt;. Take two tasks. The first
   critical decision has two cases: either these tasks concurrently access overlapping
   data in shared-memory, or they do not. If they do not, they are &lt;em&gt;isolated&lt;/em&gt;,
   and no precautions associated with racing memory updates are needed. If they do share
   data, on the other hand, then something else must give. If all concurrently accessed
   data is &lt;em&gt;immutable&lt;/em&gt;, or all functions used to interact with data are &lt;em&gt;pure&lt;/em&gt;,
   then dangerous concurrency hazards are avoided. All is well. If some data is mutable,
   however, then this is where things get tricky, and higher-level &lt;em&gt;synchronization&lt;/em&gt; is
   needed to make accesses safe. This decision tree is straightforward and clear.
&lt;/p&gt;
&lt;p&gt;
   I have listed those four attributes – isolated, immutable and pure, and synchronization
   – in a very intentional order. Thankfully, this order mirrors the natural top-down
   hierarchical architecture of most modern object- and component-based programs: we
   have large containers that communicate through well-defined interfaces, each comprised
   of layers of such containers, and somewhere towards the leaves, a fair amount of intimate
   commingling of knowledge regarding data and invariants.
&lt;/p&gt;
&lt;p&gt;
   This order also reflects the order of complexity and execution-time costs, from least
   to most. Isolation is simple, because components depend on each other in loosely-coupled
   ways, and in fact scales superiorly in a concurrent program because no synchronization
   is necessary, the “right” data structure may be chosen for the job – immutable or
   not – and locality is part-and-parcel to the architecture. Immutability at least avoids
   the morass of synchronization, which can affect programs immensely in complexity,
   runtime overheads, and write-contention for shared data. It is clear that synchronization
   is something to avoid at all costs, particularly anything done in an ad-hoc manner
   like locks.
&lt;/p&gt;
&lt;p&gt;
&lt;h1&gt;Making the concurrency
&lt;/h1&gt;
&gt;
&lt;p&gt;
   But where did all this concurrency come from, anyway?
&lt;/p&gt;
&lt;p&gt;
   It came from two things:
&lt;/p&gt;
&lt;ol&gt;
   &lt;li&gt;
      The coarse-grained breakdown of a program into isolated pieces. 
   &lt;li&gt;
      The fine-grained data parallelism. 
&lt;/ol&gt;
&lt;p&gt;
   On #1: Program fragments that are isolated are already half-way down the road to running
   concurrently as tasks. The second half of this journey, of course, is teaching them
   to interact with one another asynchronously, most frequently through message-passing
   or by sticking them into a pipeline. The details of course depend on what programming
   language you are using. It may be through agents, actors, active objects, COM objects,
   EJBs, CCR receivers, web-services, something ad-hoc built with .NET tasks, or some
   other reification. Nevertheless the isolation is common to all these.
&lt;/p&gt;
&lt;p&gt;
   On #2: Data parallelism, it turns out, often works best with mutable data structures.
   These structures must be partitionable, of course, so that tasks comprising the data
   parallel operations may operate with logically isolated chunks of this data safely,
   even if they are parts of the same physical data structure. So chunks of them are
   isolated even though they don’t appear to be. This is trivially achievable with many
   important parallel-friendly data structures like arrays, vectors, and matrices. Capturing
   this isolation in the type system is of course no small task, though region typing
   gets close (see UIUC’s Data Parallel Java).
&lt;/p&gt;
&lt;p&gt;
   But you usually don’t want these structures to be immutable, because they can be modified
   in constant-time and space if they are their classic simple mutable forms. Programmers
   doing HPC-style data-parallelism a la FORTRAN, vectorization, and GPGPU know this
   quite well. Compare this a world where we are doing data-parallelism over immutable
   data structures, where modifications often necessitate allocations or more complicated
   big-oh times due to clever techniques meant to avoid such allocations, as with persistent
   immutable data structures. This is likely less ideal. It is true that some data parallel
   operations are not in-place against mutable data – as with PLINQ – at which point
   purity, but not immutability, is key. The two are related but not identical: immutability
   pervades the construction of data structures, whereas purity pervades the construction
   of functions. But if you can get by with one copy of the data, why not do it? Particularly
   since most datasets amenable to parallel speedups are quite large.
&lt;/p&gt;
&lt;h1&gt;Immutability: the bricks, not the mortar
&lt;/h1&gt;
&lt;p&gt;
   Notice that the concurrency did not actually come from immutable data structures in
   either case, however. So what are they good for?
&lt;/p&gt;
&lt;p&gt;
   One obvious use, which has little to do with concurrency, is to enforce characteristics
   of particular data structures in a program. A translation lookup table may not have
   been meant to be written to except for initialization time, and using an immutable
   data structure is a wonderful way to enforce this intent.
&lt;/p&gt;
&lt;p&gt;
   What about concurrency? Immutable data structures facilitate sharing data amongst
   otherwise isolated tasks in an efficient zero-copy manner. No synchronization necessary.
   This is the real payoff.
&lt;/p&gt;
&lt;p&gt;
   For example, say we’ve got a document-editor and would like to launch a background
   task that does spellchecking in parallel. How will the spellchecker concurrently access
   the document, given that the user may continue editing it simultaneously? Likely we
   will use an immutable data structure to hold some interesting document state, such
   as storing text in a piece-table. OneNote, Visual Studio, and many other document-editors
   use this technique. This is zero-cost snapshot isolation.
&lt;/p&gt;
&lt;p&gt;
   Not having immutability in this particular scenario is immensely painful. Isolation
   won’t work very well. You could model the document as a task, and require the spellchecker
   to interact with it using messages. Chattiness would be a concern. And, worse, the
   spellchecker’s messages may now interleave with other messages, like a user editing
   the document. Those kinds of message-passing races are non-trivial to deal with. Synchronization
   won’t work well either. Clearly we don’t want to lock the user out of editing his
   or her document just because spellchecking is occurring. Such a boneheaded design
   is what leads to spinning donuts, bleached-white screens, and “(Not Responding)” title
   bars. But clearly we don’t want to acquire a lock and then make a full copy of the
   entire document. Perhaps we’d try to copy just what is visible on the screen. This
   is a dangerous game to play.
&lt;/p&gt;
&lt;p&gt;
   Immutability does not solve all of the problems in this scenario, however. Snapshots
   of any kind lead to a subtle issue that is familiar to those with experience doing
   multimaster, in which multiple parties have conflicting views on what “the” data ought
   to be, and in which these views must be reconciled.
&lt;/p&gt;
&lt;p&gt;
   In this particular case, the spellchecker sends the results back to the task which
   spawned it, and presumably owns the document, when it has finished checking some portion
   of the document. Because the spellchecker was working with an immutable snapshot,
   however, its answer may now be out-of-date. We have turned the need to deal with message-level
   interleaving – as described above – into the need to deal with all of the messages
   that may have interleaved within a window of time. This is where multimaster techniques,
   such as diffing and merging come into play. Other techniques can be used, of course,
   like cancelling and ignoring out-of-date results. But it is clear something intentional
   must be done.
&lt;/p&gt;
&lt;p&gt;
&lt;h1&gt;In conclusion
&lt;/h1&gt;
&gt;
&lt;p&gt;
   It is safe to say that immutability facilitates important concurrent architectures
   and algorithms. It can really help big time, for sure. But it is clearly no panacea.
   Whether mutability or immutability is the right choice for a particular data structure
   in your program, as with all things, depends.
&lt;/p&gt;
&lt;p&gt;
   It could be the case that choosing a piece-table for storing your text facilitates
   large-scales of concurrency in version two of your software application, but that
   in version one you have no use for it. Making that call ahead of time may pay in spades
   down the road, even if it comes at a marginal cost up-front. Or it could be that choosing
   an immutable data structure costs you in time and space, and you never end up exploiting
   the fact that you could have shared that particular structure in a zero-cost way across
   agents in your program.
&lt;/p&gt;
&lt;p&gt;
   One thing’s for sure: I’m glad to be programming in languages like C#, F#, Clojure,
   and Scala, where I’ve got a choice.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=7a57f623-d65d-4212-973d-29bdcf61dd3a" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,7a57f623-d65d-4212-973d-29bdcf61dd3a.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=4a49c0a0-3e4e-4d9f-b9de-599d89a94283</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,4a49c0a0-3e4e-4d9f-b9de-599d89a94283.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,4a49c0a0-3e4e-4d9f-b9de-599d89a94283.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=4a49c0a0-3e4e-4d9f-b9de-599d89a94283</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      In .NET today, readonly/initonly-ness is in the eye of the provider. Not the beholder. 
   </p>
        <p>
      Although both C# and the CLR verifier go to great pains to ensure you don't change
      a readonly/initonly field outside of its constructor (or class constructor, in the
      case of a static field), this guarantee doesn't imply what you might imagine. It means
      what it says: you can't change such fields except for in certain contexts. 
   </p>
        <p>
      If you try, C# won't let you, including forming byrefs to them: 
   </p>
        <p>
          <code>
            <blockquote>
              <pre>
v.cs(0,0): error CS0191: A readonly field cannot be assigned to (except in a constructor or a variable initializer)
v.cs(0,0): error CS0192: A readonly field cannot be passed ref or out (except in a constructor)
v.cs(0,0): error CS0198: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)
v.cs(0,0): error CS0199: A static readonly field cannot be passed ref or out (except in a static constructor)
</pre>
            </blockquote>
          </code>
        </p>
        <p>
        </p>
        <p>
      And neither will the CLR verifier: 
   </p>
        <p>
          <code>
            <blockquote>
              <pre>
[IL]: Error: [c:\v.exe : C::Main][offset 0x00000001] Cannot change initonly
    field outside its .ctor.
</pre>
            </blockquote>
          </code>
        </p>
        <p>
        </p>
        <p>
      Of course, attempting to invoke an operation on a readonly struct will make a defensive
      copy locally, and invoke the method against that. This ensures the readonly contents
      cannot change. 
   </p>
        <p>
      One unfortunate hole in this safety is with unions. You do not need unsafe code to
      break readonly, and yet the effect is the same as with an unverifiable program that
      writes to a readonly field: 
   </p>
        <p>
          <code>
            <blockquote>
              <pre>struct S1 {
    public readonly int X;
}

struct S2 {
    public int X;
}

[StructLayout(LayoutKind.Explicit)]
struct S3 {
    [FieldOffset(0)]
    public S1 A;
    [FieldOffset(0)]
    public S2 B;
}
</pre>
            </blockquote>
          </code>
        </p>
        <p>
        </p>
        <p>
      Now we can change A.X via B.X, even though A.X is supposedly readonly: 
   </p>
        <p>
          <code>
            <blockquote>
              <pre>S3 s3 = ...;
int x = s3.A.X;
s3.B.X++;
ASSERT(x == s3.A.X); // false; it is +1
</pre>
            </blockquote>
          </code>
        </p>
        <p>
        </p>
        <p>
      The same would have been true even if the field S3.A was marked readonly. 
   </p>
        <p>
      This is quite an evil trick. I have to be honest that I believe this is a CIL verification
      hole, and should produce unverifiable MSIL much like when you try to overlay structs
      containing overlapping GC references. Nevertheless, it is what it is. 
   </p>
        <p>
      Let's step back. Why does all of this matter, anyway, and what guarantees were we
      hoping that readonly would provide?
   </p>
        <p>
      It would be ideal, I assert, if the guarantee was not just "the target field can only
      be written to in the constructor", but also "the target field, once read, cannot be
      observed with a different value later on". This would not be true during construction,
      but we'd like to say it holds at all other times. 
   </p>
        <p>
      The above example throws a wrench in this idea. As does the following example. But
      this new example will be more disturbing, because the solution is not a simple verifier
      change. 
   </p>
        <p>
      What would you expect this program to print to the console? 
   </p>
        <p>
          <code>
            <blockquote>
              <pre>struct S {
    public readonly int X;

    public S(int x) { X = x; }

    public void MultiplyInto(int c, out S target) {
        System.Console.WriteLine(this.X);
        target = new S(X * c);
        System.Console.WriteLine(this.X); // same? it is, after all, readonly.
    }
}

S s = new S(42);
s.MultiplyInto(10, out s);
</pre>
            </blockquote>
          </code>
        </p>
        <p>
        </p>
        <p>
      As you may or may not have guessed, the output is "42" followed by "420". Yes, the
      value of 'this.X' changes after we have assigned to 'target' inside MultiplyTo, because
      the caller aliases the out-param with the 'this' param. Recall that parameter passing
      for structs in C# is done byref, so that these two references actually physically
      point to the same location when that call is made. The assignment to 'target', therefore,
      actually replaces the entire contents of 'this' all at once. And hence this gives
      the illusion that readonly fields are shifting. 
   </p>
        <p>
      You might be tempted to say that this can be prevented with alias analysis. But this
      is deceptively difficult to do. Consider this more complicated example: 
   </p>
        <p>
          <code>
            <blockquote>
              <pre>class C {
    public struct S S;
}

void M1(C c) {
    M2(c, out c.S);
}

void M2(C c, out S s) {
    c.S.MultiplyInto(10, out s);
}
</pre>
            </blockquote>
          </code>
        </p>
        <p>
        </p>
        <p>
      It is in no way clear inside M2 that the two aliases refer to the same location. The
      aliasing occurred higher up in the stack. Although byrefs are restricted to stack-only
      passing, making the necessary alias analysis tantalizingly close to attainable, it
      is nontrivial to say the least. Presumably we would have had to have blocked the forming
      of the byref within M1, rather than its use within M2. We could fall back to runtime
      checks, but that is also unfortunate for numerous reasons. 
   </p>
        <p>
      The moral of the story? Structs as containers of readonly values are not to be trusted,
      at least not for situations that call for bulletproof safety, such as caching values
      in the compiler rather than rereading them, because the fields are readonly. Although
      C# and the CLR do a good job at verifying readonly/initonly are done right at the
      initialization site, there are still places where these guarantees break down. Thankfully
      the byref aliasing problem does not threaten thread-safety, but the union problem
      does. And in conclusion, I do have to imagine all of this will get fixed somewhere
      down the road, it's just a matter of when and where. 
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=4a49c0a0-3e4e-4d9f-b9de-599d89a94283" />
      </body>
      <title>When is a readonly field not readonly?</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,4a49c0a0-3e4e-4d9f-b9de-599d89a94283.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/07/01/WhenIsAReadonlyFieldNotReadonly.aspx</link>
      <pubDate>Thu, 01 Jul 2010 19:41:20 GMT</pubDate>
      <description>&lt;p&gt;
   In .NET today, readonly/initonly-ness is in the eye of the provider. Not the beholder. 
&lt;/p&gt;
&lt;p&gt;
   Although both C# and the CLR verifier go to great pains to ensure you don't change
   a readonly/initonly field outside of its constructor (or class constructor, in the
   case of a static field), this guarantee doesn't imply what you might imagine. It means
   what it says: you can't change such fields except for in certain contexts. 
&lt;/p&gt;
&lt;p&gt;
   If you try, C# won't let you, including forming byrefs to them: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt;&lt;pre&gt;
v.cs(0,0): error CS0191: A readonly field cannot be assigned to (except in a constructor or a variable initializer)
v.cs(0,0): error CS0192: A readonly field cannot be passed ref or out (except in a constructor)
v.cs(0,0): error CS0198: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)
v.cs(0,0): error CS0199: A static readonly field cannot be passed ref or out (except in a static constructor)
&lt;/pre&gt;
   &lt;/blockquote&gt;&lt;/code&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
   And neither will the CLR verifier: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt;&lt;pre&gt;
[IL]: Error: [c:\v.exe : C::Main][offset 0x00000001] Cannot change initonly
    field outside its .ctor.
&lt;/pre&gt;
   &lt;/blockquote&gt;&lt;/code&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
   Of course, attempting to invoke an operation on a readonly struct will make a defensive
   copy locally, and invoke the method against that. This ensures the readonly contents
   cannot change. 
&lt;/p&gt;
&lt;p&gt;
   One unfortunate hole in this safety is with unions. You do not need unsafe code to
   break readonly, and yet the effect is the same as with an unverifiable program that
   writes to a readonly field: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt;&lt;pre&gt;struct S1 {
    public readonly int X;
}

struct S2 {
    public int X;
}

[StructLayout(LayoutKind.Explicit)]
struct S3 {
    [FieldOffset(0)]
    public S1 A;
    [FieldOffset(0)]
    public S2 B;
}
&lt;/pre&gt;
   &lt;/blockquote&gt;&lt;/code&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
   Now we can change A.X via B.X, even though A.X is supposedly readonly: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt;&lt;pre&gt;S3 s3 = ...;
int x = s3.A.X;
s3.B.X++;
ASSERT(x == s3.A.X); // false; it is +1
&lt;/pre&gt;
   &lt;/blockquote&gt;&lt;/code&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
   The same would have been true even if the field S3.A was marked readonly. 
&lt;/p&gt;
&lt;p&gt;
   This is quite an evil trick. I have to be honest that I believe this is a CIL verification
   hole, and should produce unverifiable MSIL much like when you try to overlay structs
   containing overlapping GC references. Nevertheless, it is what it is. 
&lt;/p&gt;
&lt;p&gt;
   Let's step back. Why does all of this matter, anyway, and what guarantees were we
   hoping that readonly would provide?
&lt;/p&gt;
&lt;p&gt;
   It would be ideal, I assert, if the guarantee was not just "the target field can only
   be written to in the constructor", but also "the target field, once read, cannot be
   observed with a different value later on". This would not be true during construction,
   but we'd like to say it holds at all other times. 
&lt;/p&gt;
&lt;p&gt;
   The above example throws a wrench in this idea. As does the following example. But
   this new example will be more disturbing, because the solution is not a simple verifier
   change. 
&lt;/p&gt;
&lt;p&gt;
   What would you expect this program to print to the console? 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt;&lt;pre&gt;struct S {
    public readonly int X;

    public S(int x) { X = x; }

    public void MultiplyInto(int c, out S target) {
        System.Console.WriteLine(this.X);
        target = new S(X * c);
        System.Console.WriteLine(this.X); // same? it is, after all, readonly.
    }
}

S s = new S(42);
s.MultiplyInto(10, out s);
&lt;/pre&gt;
   &lt;/blockquote&gt;&lt;/code&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
   As you may or may not have guessed, the output is "42" followed by "420". Yes, the
   value of 'this.X' changes after we have assigned to 'target' inside MultiplyTo, because
   the caller aliases the out-param with the 'this' param. Recall that parameter passing
   for structs in C# is done byref, so that these two references actually physically
   point to the same location when that call is made. The assignment to 'target', therefore,
   actually replaces the entire contents of 'this' all at once. And hence this gives
   the illusion that readonly fields are shifting. 
&lt;/p&gt;
&lt;p&gt;
   You might be tempted to say that this can be prevented with alias analysis. But this
   is deceptively difficult to do. Consider this more complicated example: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt;&lt;pre&gt;class C {
    public struct S S;
}

void M1(C c) {
    M2(c, out c.S);
}

void M2(C c, out S s) {
    c.S.MultiplyInto(10, out s);
}
&lt;/pre&gt;
   &lt;/blockquote&gt;&lt;/code&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
   It is in no way clear inside M2 that the two aliases refer to the same location. The
   aliasing occurred higher up in the stack. Although byrefs are restricted to stack-only
   passing, making the necessary alias analysis tantalizingly close to attainable, it
   is nontrivial to say the least. Presumably we would have had to have blocked the forming
   of the byref within M1, rather than its use within M2. We could fall back to runtime
   checks, but that is also unfortunate for numerous reasons. 
&lt;/p&gt;
&lt;p&gt;
   The moral of the story? Structs as containers of readonly values are not to be trusted,
   at least not for situations that call for bulletproof safety, such as caching values
   in the compiler rather than rereading them, because the fields are readonly. Although
   C# and the CLR do a good job at verifying readonly/initonly are done right at the
   initialization site, there are still places where these guarantees break down. Thankfully
   the byref aliasing problem does not threaten thread-safety, but the union problem
   does. And in conclusion, I do have to imagine all of this will get fixed somewhere
   down the road, it's just a matter of when and where. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=4a49c0a0-3e4e-4d9f-b9de-599d89a94283" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,4a49c0a0-3e4e-4d9f-b9de-599d89a94283.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=1f0b032e-5871-4749-9c74-71fe205c9bc5</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,1f0b032e-5871-4749-9c74-71fe205c9bc5.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,1f0b032e-5871-4749-9c74-71fe205c9bc5.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=1f0b032e-5871-4749-9c74-71fe205c9bc5</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <title>On partially-constructed objects</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,1f0b032e-5871-4749-9c74-71fe205c9bc5.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/06/27/OnPartiallyconstructedObjects.aspx</link>
      <pubDate>Sun, 27 Jun 2010 19:52:29 GMT</pubDate>
      <description>&lt;p&gt;
   Partially-constructed objects are a constant source of difficulty in object-oriented
   systems. These are objects whose construction has not completed in its entirety prior
   to another piece of code trying to use them. Such uses are often error-prone, because
   the object in question is likely not in a consistent state. Because this situation
   is comparatively rare in the wild, however, most people (safely) ignore or remain
   ignorant of the problem. Until one day they get bitten. 
&lt;/p&gt;
&lt;p&gt;
   Not only are partially-constructed objects a source of consternation for everyday
   programmers, they are also a challenge for language designers wanting to provide guarantees
   around invariants, immutability and concurrency-safety, and non-nullability. We shall
   see examples below why this is true. The world would be better off if partially-constructed
   objects did not exist. Thankfully there is some interesting prior art that moves us
   in this direction from which to learn. 
&lt;/p&gt;
&lt;p&gt;
&lt;h1&gt;Seeing such a beast in the wild
&lt;/h1&gt;
&gt;
&lt;p&gt;
   In what situations might you see a partially-constructed object? There are two common
   ones in C++ and C#: 
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      ‘this’ is leaked out of a constructor to some code that assumes the object has been
      initialized.&lt;/li&gt;
   &lt;li&gt;
      A failure partway through an object’s construction leads to its destructor or finalizer
      running against a partially-constructed object.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   In the first case, the rule of thumb is “don’t do that.” This is easier said than
   done. The second case, on the other hand, is a fact of life, and the rule of thumb
   is “tread with care, and be intentional.” Let’s examine both more closely. 
&lt;/p&gt;
&lt;p&gt;
&lt;h2&gt;The evils of leaking ‘this’
&lt;/h2&gt;
&gt;
&lt;p&gt;
   Leaking ‘this’ during construction to code that expects to see a fully-initialized
   object is a terrible practice. Before moving on, it’s important to remember initialization
   order in C++ and C#: base constructors run first, and then more derived constructors.
   If I have E subclasses D subclasses C, then constructing an instance of E will run
   C’s constructor, and then D’s, and then lastly E’s. Destructors in C++, of course,
   run in the reverse order. 
&lt;/p&gt;
&lt;p&gt;
   Member initializers, on the other hand, run in different orders in C++ versus C#.
   In C#, they run from most derived first, to least derived. So in the above situation,
   E’s initializers run, and then D’s, and then C’s. This happens fully before running
   the ad-hoc constructor code. In C++, however, member initializers run alongside the
   ordinary construction process. C’s member initializers run just before C’s ad-hoc
   construction code, and then D’s, and then E’s. Another difference is that C#’s initializers
   cannot access ‘this’, whereas C++’s initializers can. 
&lt;/p&gt;
&lt;p&gt;
   For example, this C# program will print E_init, D_init, C_init, C_ctor, D_ctor, and
   then E_ctor: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
using System;
class C {
    int x = M();
    public C() {
        Console.WriteLine("C_ctor");
    }
    private static int M() {
        Console.WriteLine("C_init");
        return 42;
    }
}

class D : C {
    int x = M();
    public D() : base() {
        Console.WriteLine("D_ctor");
    }
    private static int M() {
        Console.WriteLine("D_init");
        return 42;
    }
}

class E : D {
    int x = M();
    public E() : base() {
        Console.WriteLine("E_ctor");
    }
    private static int M() {
        Console.WriteLine("E_init");
        return 42;
    }
}

class Program {
    public static void Main() {
        new E();
    }
}
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   And this C++ program will print C_init, C_ctor, D_init, D_ctor, E_init, E_ctor, ~E,
   ~D, and finally ~C: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
#include 
   &lt;iostream&gt;&lt;&lt; "C_ctor" &lt;&lt; endl;   }
    ~C() { cout &lt;&lt; "~C" &lt;&lt; endl; }
    static int M() { cout &lt;&lt; "C_init" &lt;&lt; endl; return 42; }
};

struct D : C {
   int x;
    D(): x(M()) { cout &lt;&lt; "D_ctor" &lt;&lt; endl; }
    ~D() { cout &lt;&lt; "~D" &lt;&lt; endl; }
    static int M() { cout &lt;&lt; "D_init" &lt;&lt; endl; return 42; }
};

struct E : D {
   int x;
    E() : x(M()) { cout &lt;&lt; "E_ctor" &lt;&lt; endl; }
    ~E() { cout &lt;&lt; "~E" &lt;&lt; endl; }
    static int M() { cout &lt;&lt; "E_init" &lt;&lt; endl; return 42; }
};

static void main() {
    E e;
}
&lt;/pre&gt;
      using namespace std; struct C { int x; C() : x(M()) { cout 
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   It’s interesting to note that the CLR allows constructor chaining to happen in any
   order. The C# compiler emits the calls to base as the first thing a constructor does,
   but other languages can choose to do differently. The verifier ensures that a call
   has occurred somewhere in the constructor body before returning. 
&lt;/p&gt;
&lt;p&gt;
   This IL program, for example, will print E, D, and then C – the reverse of what C#
   gives us: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
.assembly extern mscorlib { }
.assembly ctor { }

.class C {
  .method public specialname rtspecialname instance void .ctor() cil managed {
    ldstr      "C"
    call       void [mscorlib]System.Console::WriteLine(string)
    ldarg.0
    call       instance void [mscorlib]System.Object::.ctor()
    ret
  }
}

.class D extends C {
  .method public specialname rtspecialname instance void .ctor() cil managed {
    ldstr      "D"
    call       void [mscorlib]System.Console::WriteLine(string)
    ldarg.0
    call       instance void C::.ctor()
    ret
  }
}

.class E extends D {
  .method public specialname rtspecialname instance void .ctor() cil managed {
    ldstr      "E"
    call       void [mscorlib]System.Console::WriteLine(string)
    ldarg.0
    call       instance void D::.ctor()
    ret
  }
}

.class Program {
  .method public static void Main() cil managed {
    .entrypoint
    newobj     instance void E::.ctor()
    pop
    ret
  }
}
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   So why is leaking ‘this’ bad, anyway? 
&lt;/p&gt;
&lt;p&gt;
   Say you’ve decided in the implementation of D’s constructor that you would like to
   stick ‘this’ into a global hash-map. Doing this sadly means other code could grab
   the pointer and begin accessing it before E’s constructor has even run. That is a
   race at-best and a ticking time-bomb in all likelihood. For example: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;&lt;int, C&gt;
class C {
    public static Dictionary s_globalLookup;
    private readonly int m_key;
    public C(int key) {
        m_key = key;
        s_globalLookup.Add(key, this);
    }
}
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   Even though we have taken great care to initialize our readonly field m_key before
   sticking ‘this’ into a dictionary, any subclasses will not have been initialized at
   this point. Only if C is sealed can we be assured of this. Another piece of code that
   grabs the element out of the hashtable and begins calling virtual methods on it, for
   example, is in a race with the completion of the initialization code for subclasses. 
&lt;/p&gt;
&lt;p&gt;
   Leaking ‘this’ isn’t always such an explicit act. Merely invoking a virtual method
   within the constructor may dispatch a more derived class’s override before the more
   derived class’s constructor has run. And therefore its state is most likely not in
   a place conducive to correct execution of that override. It is fairly common knowledge
   that invoking virtual methods during construction is an extraordinarily poor practice,
   and best avoided. 
&lt;/p&gt;
&lt;p&gt;
&lt;h2&gt;Failure to construct
&lt;/h2&gt;
&gt;
&lt;p&gt;
   Let’s move on to the second issue. Imagine we suffer an exception during construction
   of an object. Perhaps this is due to a failure to allocate resources, or maybe even
   due to argument validation. It is clear that a leaked ‘this’ object in such cases
   will be a problem, because the object will have escaped into the wild even though
   its initialization failed. Subsequent attempts to use the object will obviously pose
   problems. What is more subtle is that if the class in question declares a destructor
   (in C++) or finalizer (in C#), a problem may now be lurking. 
&lt;/p&gt;
&lt;p&gt;
   Let’s say we have the original situation shown above: C derives from D derives from
   E. Now say an exception happens within D’s constructor. At this point in time, C’s
   constructor has run to completion, D’s constructor has run partially up to the point
   of failure, and E’s has not run at all. (And, of course, in the case of C#, all member-initializers
   for all classes have actually run.) What happens to the cleanup code? 
&lt;/p&gt;
&lt;p&gt;
   In C++, only constructors that have run will have their corresponding destructors
   executed. In the above situation, where C, D, and E each declares a destructor, only
   C’s will be run during stack unwind. It is imperative, therefore, that D handles failure
   within its constructor rather than relying on the destructor. For example: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
class D : C {
    int* m_pBuf1;
    int* m_pBuf2;
public:
    D() {
        m_pBuf1 = … alloc …;
        m_pBuf2 = … alloc …;
    }
    ~D() {
        if (m_pBuf2) … free …;
        if (m_pBuf1) … free …;
    }
}
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   If the allocation destined for m_pBuf2 fails by throwing an exception, the destructor
   for D will not run, and therefore m_pBuf1 will leak. The C++ solution to this particular
   example is to use smart pointers and member initializers for the allocations, because
   successfully initialized members do get destructed, even when the constructor (or
   indeed one of the member initializers) subsequently fails. This means that destructors
   for a particular class do not have to tolerate that class’s state not having been
   fully constructed, because those destructors will never run. Destructors must not,
   however, invoke virtuals, for two obvious reasons: (a) subclasses may not have ever
   been initialized, and (b) destructors run in reverse order, and so the destruction
   code for the subclass will have already run by the time a baseclass’s destructor runs. 
&lt;/p&gt;
&lt;p&gt;
   In C#, finalizers will run, regardless of whether an object’s constructor ran fully,
   partway through, or not at all. If the object is allocated – and so long as GC.SuppressFinalize
   isn’t called on it – the finalizer runs. This distinctly means that C# finalizers
   must always be resilient to partially-constructed objects (unlike C++ destructors).
   Thankfully finalizers are a rare bird, and therefore this issue is seldom even noticed
   by .NET programmers. 
&lt;/p&gt;
&lt;p&gt;
   This issue does not arise in the case of .NET’s IDisposable pattern. If a constructor
   throws, the assignment to the target local variable does not occur. And therefore
   the variable enclosed in, say, a using statement remains null. This means that there
   is no way to possibly invoke Dispose on the object. Moreover, the allocation in using
   occurs prior to entering the try/finally block. Hence, you really had better be writing
   constructors that don’t throw and/or protecting such resources with smart-pointer-like
   things with finalizers, a la SafeHandle. 
&lt;/p&gt;
&lt;p&gt;
&lt;h1&gt;Impediments to language support
&lt;/h1&gt;
&gt;
&lt;p&gt;
   As if these weren’t sufficient cause for concern, I also mentioned earlier – and somewhat
   vaguely – that partially-constructed objects interfere with language designers’ efforts
   to add invariants, immutability and concurrency-safety, and non-nullability to the
   language. And all of these are important properties to consider in our present age
   of complexity and concurrency, so this point is worth understanding more deeply. Let’s
   look at each in-order. 
&lt;/p&gt;
&lt;p&gt;
&lt;h2&gt;Invariants and safe-points
&lt;/h2&gt;
&gt;
A partially-constructed object obviously may have broken invariants. By definition,
invariants are meant to hold at the end of construction, and so if construction never
completes, the rules of engagement are being broken. &gt;
&lt;p&gt;
   Imagine, for example: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;&lt; m_y;
    public C(int a) {
        m_x = a;
        m_y = a + 1;
    }
}
&lt;/pre&gt;
class C {
    int m_x;
    int m_y;
    invariant m_x 

   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   It is ordinarily very difficult to ensure that each instruction atomically transitions
   the state of an object from one invariant safe-point to another. A common technique
   is to define well-defined points at which invariants must hold. We might model each
   failure point as one such technique. But even in C#, the above program does not satisfy
   this constraint, because an overflow exception may be generated at the ‘m_y = a +
   1’ line. Or a thread-abort exception may be generated right in the middle of those
   two functions. Or, if addition were implemented as a JIT helper, an out-of-memory
   exception could get generated due to failure to JIT the helper function. 
&lt;/p&gt;
&lt;p&gt;
   In such cases, we’d like to say that the object does not exist. But the sad fact is
   that the object *does* exist, and if the object has acquired physical resources at
   the time of failure to construct, we must compensate and reclaim them. The ideal world
   looks a lot like object construction as transactions, where the end of construction
   is the commit-point. The state-of-the-art is very different from this, however, and
   so any static verification and theorem proving that depends on invariants on an object
   holding, well, invariantly, is subject to being broken by partially-constructed objects. 
&lt;/p&gt;
&lt;p&gt;
&lt;h2&gt;Immutability… or not
&lt;/h2&gt;
&gt;
&lt;p&gt;
   Immutability is also threatened by partially-constructed objects. Immutability is
   a one of many first class techniques for solving concurrency-safety in the language,
   so this one is quite unfortunate. 
&lt;/p&gt;
&lt;p&gt;
   In C#, for example, we might be tempted to say that a shallowly immutable type is
   one whose fields are all marked readonly. (And a deeply immutable type is one whose
   fields are all readonly, and also refer to types that are immutable.) A readonly field
   cannot be written to after construction has completed. Unfortunately, if the ‘this’
   leaks out during construction, we may see those readonly fields in an uninitialized
   or even changing state: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
class C {
    public static C s_c;
    readonly int m_x;
    public C() {
        m_x = 42;
        s_c = this;
        while (true) {
            ++m_x;
        }
    }
}
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   This is quite evidently a terrible and malicious program. C appears to be immutable,
   because it only contains readonly fields, but is quite clearly not, because the value
   of m_x is assigned to multiple times. If we had a guarantee that all readonly fields
   were definitely assigned once-and-only-once before ‘this’ can escape, then we’d be
   close to a solution. But of course we have no such guarantee. In C#, at least. 
&lt;/p&gt;
&lt;p&gt;
   A related issue is co-initialization of objects. This is interesting and relevant,
   because in such cases we actually want to leak out partially-constructed objects.
   Imagine we’d like to build a cyclic graph comprised of two nodes, A and B, each referring
   to the other. With a naïve approach to immutability, we simply cannot make this work.
   Either A must first refer to B, in which case A refers to a partially-constructed
   B; or B must first refer to A, in which case B refers to a partially-constructed A.
   Both are equally as bad. The two assignments are not atomic. 
&lt;/p&gt;
&lt;p&gt;
   Cyclic data structures are a commonly cited weakness of immutability, and an argument
   in favor of supporting partially-instantiated objects in a first class way, although
   there are approaches that can work. One example is to separate edges from nodes, and
   represent them with different data structures. We can then build the nodes A and B,
   and then build the edges A-&gt;B and B-&gt;A without needing to use cycles. 
&lt;/p&gt;
&lt;p&gt;
&lt;h2&gt;It’s not-null, or at least it wasn’t supposed to be
&lt;/h2&gt;
&gt;
&lt;p&gt;
   Tony Hoare called it his billion-dollar mistake: the introduction of nulls into a
   programming language. I think he sells himself short, however, because the absence
   of nulls in an imperative programming language – however worthy a pursuit – is actually
   a notoriously difficult to attain. 
&lt;/p&gt;
&lt;p&gt;
   Spec# is one example of a C-style language with non-nullability, in which T! represents
   a “non-null T”, for any T. Although this is done in a pleasant way conducive to C#-compatibility
   – a significant goal of Spec# -- I’d personally prefer to see the polarity switched:
   T would mean “non-null T” and T? would mean “nullable T”, for any T, reference- and
   value-types included. This is much more like Haskell’s Maybe monad, and is the syntax
   I’ll use below for illustration purposes. But I digress. 
&lt;/p&gt;
&lt;p&gt;
   Non-nullability is a wonderful invention, because it is common for 75% or more of
   the contracts and assertions in modern programs to be about a pointer being non-null
   prior to dereferencing it, both in C# and in C++. Relying on the type-system to prove
   the absence of nulls is one big step towards creating programs that are robust and
   correct out-of-the-gate, particularly for systems software where such degrees of reliability
   are important. And it cuts down on all those boilerplate contracts sprinkled throughout
   a program. Instead of: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
void M(C c, D d, E e)
    requires c != null, d != null, e != null
{
    … use(c, d, e) …
}
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   You simply say: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
void M(C c, D d, E e)
{
    … use (c, d, e) …
}
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   No opportunity to miss one, and no need for runtime checks. It’s beautiful. 
&lt;/p&gt;
&lt;p&gt;
   A problem quickly arises, however, having to do with partially-constructed objects.
   All of an object’s fields cannot possibly be non-null while the constructor is executing,
   because the object has been zero’d out and the assignments to its fields have not
   yet been made. Clearly constructor code needs to be treated “differently” somehow.
   We cannot simply live with the fact that ‘this’ escaping leads to a partially-constructed
   object leaking out into the program, because that could lead to serious errors. These
   serious errors include potential security holes, if unsafe code is manipulating the
   supposedly non-null pointer. One advantage to adding non-nullability is that runtime
   null checks can be omitted, because the type system guarantees the absence of nulls
   in certain places. In this situation, partially-constructed objects lead to holes
   in our nice type system support. Either the dynamic non-null checks are required as
   back-stop, or we’ll need some other coping technique. 
&lt;/p&gt;
&lt;p&gt;
   There are related issues with non-nullability, like with partially-populated arrays.
   Imagine we’d like to allocate an array of 1M elements of type T, and we will proceed
   to populate those elements following the array’s allocation. There’s clearly a window
   of time during which the array contains 1M nulls, and then 1M-1 nulls, …, and if we
   finish 1M-1M nulls, i.e. 0 nulls. It is only at that last transition that the array
   can be considered to contain non-null T’s. The standard technique is to use an explicit
   dynamic conversion check, or to force the creation of the list to supply all of the
   elements of the array at construction time. 
&lt;/p&gt;
&lt;p&gt;
&lt;h1&gt;Coping techniques
&lt;/h1&gt;
&gt;
&lt;p&gt;
   There are, thankfully, some interesting ways to cope with partially-constructed objects.
   There is, in fact, a spectrum of techniques, ranging from escape analysis in various
   forms, to limitations around how objects are constructed such that a partially-constructed
   one can never leak, to automatic insertion of dynamic checks to prevent the use of
   partially-constructed objects, to static annotations that treat partially-constructed
   instances as first class things in the type system. And of course there’s always the
   technique of “deal with it”, which is the one that most C++-style languages have chosen,
   including our beloved C#. 
&lt;/p&gt;
&lt;p&gt;
&lt;h2&gt;The F# approach: restrictions and dynamic checks
&lt;/h2&gt;
&gt;
&lt;p&gt;
   F#, it turns out, does a very good job in preventing partially-initialized objects.
   A first important step is that fields in F# are readonly by-default, unless you opt-in
   to mutability using the mutable keyword. Therefore data structures are mostly immutable.
   And the construction rules are meant to make it very unlikely that you’ll expose a
   partially-constructed object during construction. How so? It’s simple: such fields
   must be initialized prior to running ad-hoc construction code, and if you attempt
   to initialize them multiple times, the compiler supplies an error. In other words,
   you really have to work hard to screw yourself, unlike C++ and C# where it’s very
   easy. 
&lt;/p&gt;
&lt;p&gt;
   It’s of course possible to do some dirty tricks to publish or access a partially-initialized
   object, despite needing to work very hard at it. There is, however, a nice surprise
   awaiting us when we try. For example: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
type C() =
    abstract member virt : unit -&gt; unit
    default this.virt() = ()

type D() as this =
    inherit C()
    do this.virt()

type E =
    inherit D
    val x : int
    new() = { x = 42; }
    override this.virt() = printf "x: %d" this.x

let e = new E()
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   This example attempts to perform a virtual invocation from C before the more derived
   class has been fully initialized. This overridden virtual simply (attempts to) prints
   out the value of x. If we compile and run this program, however, we will notice that
   we get an exception: “InvalidOperationException: the initialization of an object or
   value resulted in an object or value being accessed recursively before it was fully
   initialized.” 
&lt;/p&gt;
&lt;p&gt;
   Pretty neat. The compiler will stick in the checks necessary when ‘this’ is being
   accessed, to dynamically verify that an object is not being leaked before having been
   fully initialized. The F# approach can be summed up as trying to make things airtight
   as possible statically at compile-time, but admitting that there are holes – primarily
   due to inheritance – and dealing with them by inserting dynamic runtime checks. This
   tradeoff clearly makes sense for F#, because it is attempting to attain a robust level
   of reliability around immutability. 
&lt;/p&gt;
&lt;p&gt;
   F# also adds non-nullability for the most part. Like Haskell’s Maybe monad, F# adds
   an option type that can store a single None value which lies outside of the underlying
   type’s domain to effectively represent null. Because F# is a .NET language it of course
   also needs to worry about nulls at interop boundaries with other languages like C#
   and VB. This is a great step forward; first class CLI support would be a nice next
   step. 
&lt;/p&gt;
&lt;p&gt;
   A slight variant of the F# idea is to initialize data up the whole class hierarchy
   in one pass, and then run ad-hoc construction code in a second pass in the usual way.
   So long as readonly data can be initialized without running the ad-hoc construction
   code, this helps to statistically cut down on the chances for accidental leaking of
   ‘this’. 
&lt;/p&gt;
&lt;p&gt;
&lt;h2&gt;Type system: T versus notconstructed-T
&lt;/h2&gt;
&gt;
&lt;p&gt;
   We can model two kinds of T in the type-system: T and notconstructed-T. The constructor
   for any type T would then see the ‘this’ pointer as an notconstructed-T, and everything
   else – by default – sees ordinary T’s. 
&lt;/p&gt;
&lt;p&gt;
   What good does this distinction do? It enables us to add verification and restrictions
   around the use of notconstructed-T’s and limitations to the conversion between the
   two types. See &lt;a href="http://portal.acm.org/citation.cfm?id=949305.949332"&gt;this
   paper by Manuel Fahndrich and Rustan Leino&lt;/a&gt; for an example of how this approach
   was taken in Spec#’s non-nullability work. 
&lt;/p&gt;
&lt;p&gt;
   For example, we can prohibit conversion between T and notconstructed-T altogether,
   thereby disallowing escaping ‘this’ references altogether. If the type of ‘this’ within
   a constructor is different than all other references to type T, and they are not convertible,
   we’ve successfully walled the problem off in the type system. This protects against
   erroneous method calls, so that a constructor could not call any of its own methods,
   because these methods expect an ordinary T whereas the constructor only has a notconstructed-T.
   And because you cannot state notconstructed-T in the language, you cannot let one
   leak by storing it into a field. 
&lt;/p&gt;
&lt;p&gt;
   We could add more sophisticated support, by allowing a programmer to explicitly tag
   certain non-field references as T-notconstructed. This makes the concept first-class
   in the language, and allows one to explicitly declare the fact that code is interacting
   with a partially-constructed T: 
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt; &lt;blockquote&gt; &lt;pre&gt;
class C
{
    int m_x;
    public C() {
        m_x = V();
    }
    protected virtual int V() notconstructed {
        … I know to be careful …
    }
}
&lt;/pre&gt;
   &lt;/blockquote&gt; &lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   In this example, the programmer has annotated V with ‘notconstructed’. This enables
   the call from the constructor because the method’s ‘this’ is an uninitialized-T, and
   serves as a warning to the programmer that he or she should take care, much like the
   code written inside a constructor. 
&lt;/p&gt;
&lt;p&gt;
   We must also decide whether fields are offlimits via notconstructed-T’s. If yes, we
   can add F#-style dynamic checks for initialization, but only for attempted accesses
   against notconstructed object’s fields. This is nice because it means the scope of
   dynamic checks are limited, and used in a pay-for-play manner. And we could even enable
   a programmer to sidestep the error by stating at the use-site that they understand
   a particular field access may be to uninitialized memory, like Field.ReadMaybeUninitialized(&amp;m_field). 
&lt;/p&gt;
&lt;p&gt;
   To be honest, the reason this approach has likely not yet seen widespread use is that
   the cost is not commensurate with the benefit. At least, in my opinion. To make something
   like partially-constructed objects a first-class concept in a programming language,
   programmers would need to want to know where they are dealing with them. For systems
   programmers, this makes sense. For many other programmers, it would be useless ceremony
   with no perceived value. And yet the initial approach where nothing new needed to
   be stated, but yet escaping ‘this’ was prevented, blocks certain patterns of legal
   use. This is where theory and practice run up against one another. There is, however,
   presumably a nice middle ground awaiting discovery. 
&lt;/p&gt;
&lt;p&gt;
&lt;h1&gt;Winding down
&lt;/h1&gt;
&gt;
&lt;p&gt;
   I meant for this to be a short post. But the topic really is fascinating, and has
   been coming up time and time again as we do language work here at Microsoft. But it
   is truly fascinating mainly because, like nulls, the problem is widespread yet tolerable,
   and most C++ and C# programmers learn the rules and make do. Partially-constructed
   objects are a major blemish, but not a crisis that threatens the complete abandonment
   of imperative programming. 
&lt;/p&gt;
&lt;p&gt;
   I do believe language trends indicate that more will move away from C++-style object
   initialization and related issues, and more towards immutability and treating data
   and its initialization separately from imperative ad-hoc initialization code. Haskell,
   F#, and Clojure, for example, show us some promising paths forward. There are a plethora
   of other attempts at solving related problems, and I unfortunately could only scratch
   the surface. 
&lt;/p&gt;
&lt;p&gt;
   Although these techniques are not new, the primary question – to me – is how close
   to “the metal” in systems programming these concepts can be made to work. Typically
   for those situations, you need to rely on a mixture of static verification and complete
   freedom, because the dynamic checking is too costly and the code to work around overly-limiting
   static verification also adds too much cost. But as soon as you add complete freedom
   into the picture, you run into partially-constructed objects as a consequence, and
   all the issues I’ve mentioned above. 
&lt;/p&gt;
&lt;p&gt;
   Anyway, I hope it was interesting. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=1f0b032e-5871-4749-9c74-71fe205c9bc5" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,1f0b032e-5871-4749-9c74-71fe205c9bc5.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=94778838-3e27-4788-a935-006ae9c8ef94</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,94778838-3e27-4788-a935-006ae9c8ef94.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,94778838-3e27-4788-a935-006ae9c8ef94.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=94778838-3e27-4788-a935-006ae9c8ef94</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.bluebytesoftware.com/blog/2010/01/03/ABriefRetrospectiveOnTransactionalMemory.aspx">My
      article about Transactional Memory</a> (TM) was picked up by a few news feeds recently.
   </p>
        <p>
      If I had known this would occur, I would have written it with greater precision. 
      Because my article presents a mixture of technical challenges interspersed among more
      subjective and cultural issues, I am sure it is difficult to tease out my intended
      conclusion.  To summarize, I merely believe adding TM to a shared memory
      architecture alone is insufficient.
   </p>
        <p>
      Indeed, I remain a big fan of transactions.  Atomicity, consistency, and isolation,
      and coming up with strategies for achieving all three in tandem, are part and parcel
      to architecting software.
   </p>
        <p>
      After <a href="http://www.infoq.com/presentations/liskov-power-of-abstraction">watching
      Barbara Liskov's OOPSLA Turing Award reprise</a>, I decided to reacquaint myself with <a href="http://portal.acm.org/citation.cfm?id=42399">some
      old Argus papers</a> this weekend.  It has been some time since I last read them. 
      Argus was Liskov's language for distributed programming and her follow-on to CLU. 
      As with most research done by brilliant people, the work was way ahead of its time,
      has appeared in ad-hoc incarnations and permutations over time, and remains relevant
      today.  This research is particularly interesting to work that my team is doing
      right now, especially its notion of guardians.  And it is relevant to the TM
      discussion too.
   </p>
        <p>
      The Argus approach of using isolation to coarsely partition state and operations into
      independent bubbles, and then communicating asynchronously between the so-called
      guardians that are responsible for this state, is an architecture that is common
      among most highly concurrent programs.  This aids state management and fault
      tolerance.  Argus makes an interesting observation that, although guardians may
      be sent messages concurrently -- and indeed activities themselves may even introduce
      local concurrency -- manipulation of state can be done safely and even in parallel
      thanks to transactions.
   </p>
        <p>
      The requirement is that messages are atomic and commute.  Transactions, it turns
      out, are a convenient way of implementing this requirement.
   </p>
        <p>
      You will observe a similar architecture in other places, including in some languages
      that have adopted TM.  Haskell has moved in this direction.  Everything
      is purely functional and so, of course, no state is mutated in an unsafe way by default. 
      However, with the introduction of concucrrency comes mutable cells for message passing
      and with parallelism comes indeterminism.  You can push the state management
      problem up indefinitely, but at the top there are almost always mutable operations
      on real-world state (even if it is "just I/O").  Haskell programs have a safe
      architecture to begin with, and it is the intentional and careful addition of specific
      facilities that forces one to focus on the problematic seams.  One could say
      that Haskell starts clean and stays clean, versus most shared memory-based languages
      which start dirty and try to attain cleanliness (at least when it comes to concurrency).
   </p>
        <p>
      Why aren't transactions sufficient, then, given that the I in ACID stands for Isolation? 
      You wouldn't model a database as one flat table in which each row is a single byte,
      however, would you?  As you begin to decompose your program into isolated state,
      your bubbles (or guardians) are the tables, and your objects are the rows.  This
      is just an analogy but I find it useful to think in these terms.  Taking a bunch
      of intermingled state and pouring transactions on top is not going to give you this
      nicely partitioned separation of state which has proven to be the lifeblood of concurrency.
   </p>
        <p>
      Even once you've attained a more isolated architecture, however, transactions are
      not a panacea.  They are just one of many viable state management techniques
      in a programmer's arsenal, hierarchical state machines being another notable example. 
      And in fact, many of the problems I mentioned in the TM article are still worrisome
      even when you start from the right place.  From within a guardian, you may wish
      to enlist the aid of another unrelated guardian to perform a coordinated atomic activity,
      because a higher level invariant relationship between them must be preserved. 
      Or an application which composes multiple guardians may wish to do so atomically. 
      Even Argus required manual compensation for such things.  This can be solved
      in part by DTC.  But experience suggests that continuing to push the enlistment
      scope one level higher eventually leads to substantial problems.  A topic for
      another day, I suppose.
   </p>
        <p>
      My primary conclusion is that TM is a great complement to highly concurrent programs,
      but only so long as you start from the right place.  The Argus and Haskell approaches
      are conducive to large-scale concurrency, but it is primarily because of the natural
      isolation those models provide; the addition of transactions address problems that
      remain after taking that step.  But without that first step, they would have
      gotten nowhere.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=94778838-3e27-4788-a935-006ae9c8ef94" />
      </body>
      <title>More thoughts on transactional memory</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,94778838-3e27-4788-a935-006ae9c8ef94.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/05/17/MoreThoughtsOnTransactionalMemory.aspx</link>
      <pubDate>Mon, 17 May 2010 04:48:58 GMT</pubDate>
      <description>&lt;p&gt;
   &lt;a href="http://www.bluebytesoftware.com/blog/2010/01/03/ABriefRetrospectiveOnTransactionalMemory.aspx"&gt;My
   article about Transactional Memory&lt;/a&gt; (TM) was picked up by a few news feeds recently.
&lt;/p&gt;
&lt;p&gt;
   If I had known this would occur, I would have written it with greater precision.&amp;nbsp;
   Because my article presents a mixture of technical challenges interspersed among more
   subjective and cultural issues, I am sure it is difficult to tease out my intended
   conclusion.&amp;nbsp; To summarize,&amp;nbsp;I merely believe adding TM to a shared memory
   architecture alone is insufficient.
&lt;/p&gt;
&lt;p&gt;
   Indeed, I remain a big fan of transactions.&amp;nbsp; Atomicity, consistency, and isolation,
   and coming up with strategies for achieving all three in tandem, are part and parcel
   to architecting software.
&lt;/p&gt;
&lt;p&gt;
   After &lt;a href="http://www.infoq.com/presentations/liskov-power-of-abstraction"&gt;watching
   Barbara Liskov's OOPSLA Turing Award reprise&lt;/a&gt;, I decided to reacquaint myself with &lt;a href="http://portal.acm.org/citation.cfm?id=42399"&gt;some
   old Argus papers&lt;/a&gt; this weekend.&amp;nbsp; It has been some time since I last read them.&amp;nbsp;
   Argus was Liskov's language for distributed programming and her follow-on to CLU.&amp;nbsp;
   As with most research done by brilliant people, the work was way ahead of its time,
   has appeared in ad-hoc incarnations and permutations over time, and remains relevant
   today.&amp;nbsp; This research is particularly interesting to work that my team is doing
   right now, especially its notion of guardians.&amp;nbsp; And it is relevant to the TM
   discussion too.
&lt;/p&gt;
&lt;p&gt;
   The Argus approach of using isolation to coarsely partition state and operations into
   independent bubbles, and then communicating asynchronously between&amp;nbsp;the so-called
   guardians&amp;nbsp;that are responsible for this state, is an architecture that is common
   among most highly concurrent programs.&amp;nbsp; This aids state management and fault
   tolerance.&amp;nbsp; Argus makes an interesting observation that, although guardians may
   be sent messages concurrently -- and indeed activities themselves may even introduce
   local concurrency -- manipulation of state can be done safely and even in parallel
   thanks to transactions.
&lt;/p&gt;
&lt;p&gt;
   The requirement is that messages are atomic and commute.&amp;nbsp; Transactions, it turns
   out,&amp;nbsp;are a convenient way&amp;nbsp;of implementing this requirement.
&lt;/p&gt;
&lt;p&gt;
   You will observe a similar architecture in other places, including in some languages
   that have adopted TM.&amp;nbsp; Haskell has moved in this direction.&amp;nbsp; Everything
   is purely functional and so, of course, no state is mutated in an unsafe way by default.&amp;nbsp;
   However, with the introduction of concucrrency comes mutable cells for message passing
   and with parallelism comes indeterminism.&amp;nbsp; You can push the state management
   problem up indefinitely, but at the top there are almost always mutable operations
   on real-world state (even if it is "just I/O").&amp;nbsp; Haskell programs have a safe
   architecture to begin with, and it is the intentional and careful addition of specific
   facilities that forces one to focus on the problematic seams.&amp;nbsp; One could say
   that Haskell starts clean and stays clean, versus most shared memory-based languages
   which start dirty and try to attain cleanliness (at least when it comes to concurrency).
&lt;/p&gt;
&lt;p&gt;
   Why aren't transactions sufficient, then, given that the I in ACID stands for Isolation?&amp;nbsp;
   You wouldn't model a database as one flat table in which each row is a single byte,
   however, would you?&amp;nbsp; As you begin to decompose your program into isolated state,
   your bubbles (or guardians) are the tables, and your objects are the rows.&amp;nbsp; This
   is just an analogy but I find it useful to think in these terms.&amp;nbsp; Taking a bunch
   of intermingled state and pouring transactions on top is not going to give you this
   nicely partitioned separation of state which has proven to be&amp;nbsp;the lifeblood of&amp;nbsp;concurrency.
&lt;/p&gt;
&lt;p&gt;
   Even once you've attained a more isolated architecture, however, transactions are
   not a panacea.&amp;nbsp; They are just one of many viable state management techniques
   in a programmer's arsenal, hierarchical state machines being another notable example.&amp;nbsp;
   And in fact, many of the problems I mentioned in the TM article are still worrisome
   even when you start from the right place.&amp;nbsp; From within a guardian, you may wish
   to enlist the aid of another unrelated guardian to perform a coordinated atomic activity,
   because a higher level invariant relationship between them must be preserved.&amp;nbsp;
   Or an application which composes multiple guardians may wish to do so atomically.&amp;nbsp;
   Even Argus required manual compensation for such things.&amp;nbsp; This can be solved
   in part by DTC.&amp;nbsp; But experience suggests that continuing to push the enlistment
   scope one level higher eventually leads to substantial problems.&amp;nbsp; A topic for
   another day, I suppose.
&lt;/p&gt;
&lt;p&gt;
   My primary conclusion is that TM is a great complement to highly concurrent programs,
   but only so long as you start from the right place.&amp;nbsp; The Argus and Haskell approaches
   are conducive to large-scale concurrency, but it is primarily because of the natural
   isolation those models provide; the addition of transactions address problems that
   remain after taking that step.&amp;nbsp; But without that first step, they would have
   gotten nowhere.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=94778838-3e27-4788-a935-006ae9c8ef94" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,94778838-3e27-4788-a935-006ae9c8ef94.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=ae76cfe5-e205-437a-a2e0-cd91f8b13654</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,ae76cfe5-e205-437a-a2e0-cd91f8b13654.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,ae76cfe5-e205-437a-a2e0-cd91f8b13654.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=ae76cfe5-e205-437a-a2e0-cd91f8b13654</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      We use static analysis very heavily in my project here at Microsoft, as a way of finding
      bugs and/or enforcing policies that would have otherwise gone unenforced.  Many
      of the analyses we rely on are in fact minor extensions to the CLR type system, and
      verge on “effect typing”, an intriguing branch of type systems research that has matured
      significantly over the years. 
   </p>
        <p>
      Many of these annotations are done on methods, rather than types.  A few examples
      include:
   </p>
        <ol>
          <li>
         [MayBlock] indicates that a method is free to call methods that might block. 
      </li>
          <li>
         [NoAllocations] indicates that a method is neither allowed to allocate, nor call another
         method that might allocate 
      </li>
          <li>
         [Throws(...)] indicates that a method is allowed to throw an exception of a type in
         the set { … }, or call other methods that may throw exceptions in the set { … }. 
      </li>
          <li>
         And so on.</li>
        </ol>
        <p>
      It turns out there’s a general way for handling these annotations.  And indeed,
      you will quickly find that pursuing ad-hoc solutions to each independently leads to
      troubles.  We shall briefly look at the generalization.
   </p>
        <p>
      We must first observe that each falls into one of two categories: additive or subtractive. 
      MayBlock and Throws are additive.  They say what is permitted.  NoAllocations,
      on the other hand, is subtractive, because the annotation declares what is not permitted. 
      This distinction, we shall see, is crucial.
   </p>
        <p>
      First we can imagine that each distinct effect shown above has a distinct effect type.
   </p>
        <p>
      The types EMayBlock, ENoAllocations, and EThrows correspond to the annotations above. 
      This will permit us to model effects using subtyping polymorphism.  We will use
      the usual notation, i.e. “S &lt;: T” means “S is a subtype of T”, or “a S is
      substitutable in place of a T”.  For example, String &lt;: Object.  Throws
      is special, because it has a type hierarchy of its own beneath the root type. 
      As you might guess, this hierarchy is infinite in size and is comprised of each possible
      permutation of exception types.
   </p>
        <p>
      There are two special kinds of effects: the null effect (ENil), and a set of other
      effects (EMany).  The latter permits us to create a new, unique effect type merely
      by concatenating a list of other effect types.
   </p>
        <p>
      Each method is then given an EMany effect type containing its full set of effect types. 
      For example:
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
            <code>[MayBlock, Throws(typeof(FileNotFoundException)), NoAllocations]<br />
      void M() { … }</code>
          </p>
        </blockquote>
        <p>
      Is given the distinct effect type EMany { EMayBlock, EThrows(typeof(FileNotFoundException)),
      ENoAllocations }.
   </p>
        <p>
      We should make one generalization before moving on.  ENil ~ EMany { }. 
      In other words, having no effects is equivalent to a list of no effects.  Furthermore,
      EMany { } ~ EMany { ENil }.  In other words, having a list of no effects is equivalent
      to having no effects.
   </p>
        <p>
      Now we are ready to weave everything together.  The main question confronting
      us is as follows: What is the subtyping relationship between the various effect types,
      including the null and list types?
   </p>
        <p>
      The easiest to do away with is the EMany type.  Given two EMany types E and F,
      then E &lt;: F if, for all effects T in E’s type set, there exists an effect type
      U in F’s type set such that T &lt;: U.  In simpler terms, a list is a subtype
      of another list so long as all of its components are also subtypes of a component
      of the other.  This is very abstract, but we shall see soon why it is useful.
   </p>
        <p>
      Now we get to see why the additive and subtractive distinctions are so important:
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
      Given an additive effect type EAdditive, we say ENil &lt;: EAdditive.
   </p>
          <p>
      Given a subtractive effect type ESubtractive, we say ESubtractive &lt;: ENil.
   </p>
        </blockquote>
        <p>
      The first statement says that a method with no effects is substitutable for a method
      with additive effects, and the second says that a method with subtractive effects
      is substitutable for a method with no effects.  The corollaries are perhaps just
      as important.  A method with additive effects cannot take the place of a method
      with no effects, whereas a method with subtractive effects can.
   </p>
        <p>
      For the simple single-effect case, effects depicted in this way represent points on
      a line, where ENil is zero, subtractive effects are negative integers, and additive
      effects are positive integers.  The lattice obviously becomes rather complicated
      as many effects accumulate.
   </p>
        <p>
      Where does substitutability come up with respect to methods, anyway, you may ask? 
      The first is in determining which other methods can be called.  If a method
      M with effects E is trying to call another method N with effects F, this is permitted
      so long as F &lt;: E.  The next is in virtuals and overriding.  A virtual
      with effects E may be overridden by a method with effects F so long as F &lt;: E. 
      The following example illustrates this idea, in addition to the composition of the
      subtyping rules we have shown so far:
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <code>
            <p>
      class C<br />
      {<br />
          public virtual void M() {}<br />
          [MayBlock, NoAllocations]<br />
          public virtual void N() {}<br />
      }
   </p>
            <p>
      class D : C<br />
      {<br />
          [MayBlock, NoAllocations]<br />
          public override void M() {}<br />
          public override void N() {}<br />
      }
   </p>
          </code>
        </blockquote>
        <p>
      In this example, the four methods are given the following effect types:
   </p>
        <ul>
          <li>
         C::M gets EMany { ENil }, or just ENil. 
      </li>
          <li>
         C::N gets EMany { MayBlock, NoAllocations }. 
      </li>
          <li>
         D::M gets EMany { MayBlock, NoAllocations }. 
      </li>
          <li>
         D::N gets EMany { ENil }, or just ENil.</li>
        </ul>
        <p>
      What does all this gibberish mean?  Well it’s straightforward and intuitive,
      actually.
   </p>
        <p>
      We are attempting to add the MayBlock and NoAllocations effects to the overridden
      M method which has none.  Because MayBlock is additive, this is illegal (someone
      might call C::M thinking the code will not block), whereas it is OK for NoAllocations
      (calls through D::M are assured no allocations will happen, even though calls through
      C::M are guaranteed no such thing).  Similarly, we are attempting to remove both
      effects from the overridden N method.  Because MayBlock is additive, this is
      OK (M isn’t required to block, even though calls through C::M may suspect it of doing
      so), whereas it is decidedly not OK for NoAllocations (calls through C::M will reasonable
      assume allocations do not happen, whereas D::M would be free to perform them). 
      It may take some thought to convince yourself that this is correct, but I hope that
      you find that it is.  All of this works because of the subtyping of effect types.
   </p>
        <p>
      All of this works similarly with delegates.  The source delegate signature is
      akin to the base class in the above example, whereas the target method being bound
      to is like the override.
   </p>
        <p>
      Things get a little more complicated when considering the EThrows effect.  It
      is additive, so it is of course true that ENil &lt;: EThrows(*).  However, what
      if we have two different EThrows, and wish to inquire about substitutability of one
      in place of the other?  We can come up with a simple rule that is general purpose
      for all set-of-type kinds of effects.  Namely, consider two instances A and B
      of the same effect type:
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
      Given an additive effect type EAdditive, then A &lt;: B if, for all types T in A’s
      set-of-types, there exists a type U in B’s set-of-types such that T &lt;: U.
   </p>
          <p>
      Given a subtractive effect type ESubtractive, then A &lt;: B if, for all types T in
      A’s set-of-types, there exists a type U in B’s set-of-types such that U &lt;:
      T.
   </p>
        </blockquote>
        <p>
      These sound quite similar, except that they end differently (i.e. T &lt;: U vs. U
      &lt;: T).  We may illustrate the additive case with EThrows; to illustrate the
      subtractive case, let us imagine we can declare a ENoAllocations effect type that
      specifies which precise types may not be allocated:
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <code>
            <p>
      class A{}<br />
      class B : A{}
   </p>
            <p>
      class C<br />
      {<br />
          [Throws(typeof(Exception)), NoAllocations(typeof(A))]<br />
          public virtual void M() {}<br />
          [Throws(typeof(FileNotFoundException)), NoAllocations(typeof(B))]<br />
          public virtual void N() {}<br />
      }
   </p>
            <p>
      class D : C<br />
      {<br />
          [Throws(typeof(FileNotFoundException)), NoAllocations(typeof(B))]<br />
          public override void M() {}<br />
          [Throws(typeof(Exception)), NoAllocations(typeof(A))]<br />
          public override void N() {}<br />
      }
   </p>
          </code>
        </blockquote>
        <p>
      The results should not be surprising.  D::M overrides C::M’s exception list,
      by being more specific and declaring that FileNotFoundException is thrown instead
      of just Exception.  This is OK.  Whereas D::N overrides C::N’s list by being
      more general purpose, specifying Exception instead of FileNotFoundException. 
      This is clearly not OK.  The NoAllocations type works in exactly the reverse. 
      D::M attempts to prohibit allocations of B, but this is merely one possible subtype
      of the base method C::M’s declaration of A, and therefore this is illegal.  Whereas
      D::N ensures no instances of A are allocated, which of course subsumes the base method
      C::N’s declaration that no B’s are allocated.
   </p>
        <p>
      Everything gets a little more interesting when you consider generics.  For example,
      how would we type a general purpose Map method?  (This pattern arises quite frequently.) 
      We would presumably want it to somehow “acquire” the effects of the delegate it invokes
      on all elements in a list.  For example:
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <code>
            <p>
      U[] Map&lt;T, U&gt;(T[] input, Func&lt;T, U&gt; func);
   </p>
          </code>
        </blockquote>
        <p>
      This declaration is stronger than necessary.  The Func&lt;T, U&gt; class – prepackaged
      with the .NET Framework – does not have any effects on it.  So it may not, for
      example, bind to a method that has any additive effects like Throws on it.  This
      is rather unfortunate.
   </p>
        <p>
      To solve this we could imagine treating effects with parametric polymorphism:
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <code>
            <p>
      [Effects(E)]<br />
      U Func&lt;T, U, [EffectParameter] E&gt;(T x);
   </p>
          </code>
        </blockquote>
        <p>
      This fictitious syntax merely says that Func can be instantiated with an effect type
      E, and that the Func “method” itself acquires the effect E.  (Admittedly I should
      stop using faux-attribute syntax for illustrations since we’ve reached this level
      of language integration.)  Now Map can be declared as such:
   </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <code>
            <p>
      [Effects(E)]<br />
      U[] Map&lt;T, U, [EffectParameter] E&gt;(T[] input, Func&lt;T, U, E&gt; func);
   </p>
          </code>
        </blockquote>
        <p>
      This says that Map has the same effects as the Func that is supplied as an argument. 
      It turns out that we may want to extend this further, by enabling symbolic manipulation
      of effects.  We may wish, for example, to specify that the Func is not allowed
      to block, by stating it does not have [MayBlock] in it.  You could imagine using
      something very similar to generic constraints to achieve this.  It is also interesting
      to allow concatenation of multiple effect types, both through partial and full specialization. 
      For example, Map above may clearly have effects of its own.  You also tend to
      want generic constraints like, 'where E : F', which of course just depends on the
      aforementioned subtyping rules.  And of course C# 4.0's co- and contravariance
      can be applied to effects too.
   </p>
        <p>
      Anyway, I have probably gone beyond most readers’ interest level in this subject. 
      Things sure do get very interesting when you allow symbolic manipulation of effects. 
      They get even more interesting when you begin to think of types as having “permissible
      effects” attached to them.  However, the main thing I wanted to point out with
      this brief article is that this pattern arises quite frequently.  And despite
      everyone struggling through what seem to be odd corner cases as they develop ad-hoc
      solutions, there really is a sound generalization behind it all.  Many languages
      have first class effect typing, and I have found it liberating to think of many of
      these type system annotations through that lens.  Perhaps you shall too.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=ae76cfe5-e205-437a-a2e0-cd91f8b13654" />
      </body>
      <title>From Simple Annotations for Analysis to Effect Typing</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,ae76cfe5-e205-437a-a2e0-cd91f8b13654.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/04/25/FromSimpleAnnotationsForAnalysisToEffectTyping.aspx</link>
      <pubDate>Sun, 25 Apr 2010 16:35:15 GMT</pubDate>
      <description>&lt;p&gt;
   We use static analysis very heavily in my project here at Microsoft, as a way of finding
   bugs and/or enforcing policies that would have otherwise gone unenforced.&amp;nbsp; Many
   of the analyses we rely on are in fact minor extensions to the CLR type system, and
   verge on “effect typing”, an intriguing branch of type systems research that has matured
   significantly over the years. 
&lt;/p&gt;
&lt;p&gt;
   Many of these annotations are done on methods, rather than types.&amp;nbsp; A few examples
   include:
&lt;/p&gt;
&lt;ol&gt;
   &lt;li&gt;
      [MayBlock] indicates that a method is free to call methods that might block. 
   &lt;li&gt;
      [NoAllocations] indicates that a method is neither allowed to allocate, nor call another
      method that might allocate 
   &lt;li&gt;
      [Throws(...)] indicates that a method is allowed to throw an exception of a type in
      the set { … }, or call other methods that may throw exceptions in the set { … }. 
   &lt;li&gt;
      And so on.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
   It turns out there’s a general way for handling these annotations.&amp;nbsp; And indeed,
   you will quickly find that pursuing ad-hoc solutions to each independently leads to
   troubles.&amp;nbsp; We shall briefly look at the generalization.
&lt;/p&gt;
&lt;p&gt;
   We must first observe that each falls into one of two categories: additive or subtractive.&amp;nbsp;
   MayBlock and Throws are additive.&amp;nbsp; They say what is permitted.&amp;nbsp; NoAllocations,
   on the other hand, is subtractive, because the annotation declares what is not permitted.&amp;nbsp;
   This distinction, we shall see, is crucial.
&lt;/p&gt;
&lt;p&gt;
   First we can imagine that each distinct effect shown above has a distinct effect type.
&lt;/p&gt;
&lt;p&gt;
   The types EMayBlock, ENoAllocations, and EThrows correspond to the annotations above.&amp;nbsp;
   This will permit us to model effects using subtyping polymorphism.&amp;nbsp; We will use
   the usual notation, i.e. “S &amp;lt;: T” means “S is a subtype of T”, or&amp;nbsp;“a S is
   substitutable in place of a T”.&amp;nbsp; For example, String &amp;lt;: Object.&amp;nbsp; Throws
   is special, because it has a type hierarchy of its own beneath the root type.&amp;nbsp;
   As you might guess, this hierarchy is infinite in size and is comprised of each possible
   permutation of exception types.
&lt;/p&gt;
&lt;p&gt;
   There are two special kinds of effects: the null effect (ENil), and a set of other
   effects (EMany).&amp;nbsp; The latter permits us to create a new, unique effect type merely
   by concatenating a list of other effect types.
&lt;/p&gt;
&lt;p&gt;
   Each method is then given an EMany effect type containing its full set of effect types.&amp;nbsp;
   For example:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
   &lt;code&gt;[MayBlock, Throws(typeof(FileNotFoundException)), NoAllocations]&lt;br&gt;
   void M() { … }&lt;/code&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Is given the distinct effect type EMany { EMayBlock, EThrows(typeof(FileNotFoundException)),
   ENoAllocations }.
&lt;/p&gt;
&lt;p&gt;
   We should make one generalization before moving on.&amp;nbsp; ENil ~ EMany { }.&amp;nbsp;
   In other words, having no effects is equivalent to a list of no effects.&amp;nbsp; Furthermore,
   EMany { } ~ EMany { ENil }.&amp;nbsp; In other words, having a list of no effects is equivalent
   to having no effects.
&lt;/p&gt;
&lt;p&gt;
   Now we are ready to weave everything together.&amp;nbsp; The main question confronting
   us is as follows: What is the subtyping relationship between the various effect types,
   including the null and list types?
&lt;/p&gt;
&lt;p&gt;
   The easiest to do away with is the EMany type.&amp;nbsp; Given two EMany types E and F,
   then E &amp;lt;: F if, for all effects T in E’s type set, there exists an effect type
   U in F’s type set such that T &amp;lt;: U.&amp;nbsp; In simpler terms, a list is a subtype
   of another list so long as all of its components are also subtypes of a component
   of the other.&amp;nbsp; This is very abstract, but we shall see soon why it is useful.
&lt;/p&gt;
&lt;p&gt;
   Now we get to see why the additive and subtractive distinctions are so important:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
   Given an additive effect type EAdditive, we say ENil &amp;lt;: EAdditive.
&lt;/p&gt;
&lt;p&gt;
   Given a subtractive effect type ESubtractive, we say ESubtractive &amp;lt;: ENil.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   The first statement says that a method with no effects is substitutable for a method
   with additive effects, and the second says that a method with subtractive effects
   is substitutable for a method with no effects.&amp;nbsp; The corollaries are perhaps just
   as important.&amp;nbsp; A method with additive effects cannot take the place of a method
   with no effects, whereas a method with subtractive effects can.
&lt;/p&gt;
&lt;p&gt;
   For the simple single-effect case, effects depicted in this way represent points on
   a line, where ENil is zero, subtractive effects are negative integers, and additive
   effects are positive integers.&amp;nbsp; The lattice obviously becomes rather complicated
   as many effects accumulate.
&lt;/p&gt;
&lt;p&gt;
   Where does substitutability come up with respect to methods, anyway, you may ask?&amp;nbsp;
   The first is in determining which other methods can be called.&amp;nbsp;&amp;nbsp;If a method
   M&amp;nbsp;with effects E is trying to call another method N with effects F, this is permitted
   so long as F &amp;lt;: E.&amp;nbsp; The next is in virtuals and overriding.&amp;nbsp; A virtual
   with effects E may be overridden by a method with effects F so long as F &amp;lt;: E.&amp;nbsp;
   The following example illustrates this idea, in addition to the composition of the
   subtyping rules we have shown so far:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;code&gt; 
&lt;p&gt;
   class C&lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual void M() {}&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; [MayBlock, NoAllocations]&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual void N() {}&lt;br&gt;
   }
&lt;/p&gt;
&lt;p&gt;
   class D : C&lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; [MayBlock, NoAllocations]&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public override void M() {}&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public override void N() {}&lt;br&gt;
   }
&lt;/p&gt;
&lt;/code&gt;&lt;/blockquote&gt; 
&lt;p&gt;
   In this example, the four methods are given the following effect types:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      C::M gets EMany { ENil }, or just ENil. 
   &lt;li&gt;
      C::N gets EMany { MayBlock, NoAllocations }. 
   &lt;li&gt;
      D::M gets EMany { MayBlock, NoAllocations }. 
   &lt;li&gt;
      D::N gets EMany { ENil }, or just ENil.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   What does all this gibberish mean?&amp;nbsp; Well it’s straightforward and intuitive,
   actually.
&lt;/p&gt;
&lt;p&gt;
   We are attempting to add the MayBlock and NoAllocations effects to the overridden
   M method which has none.&amp;nbsp; Because MayBlock is additive, this is illegal (someone
   might call C::M thinking the code will not block), whereas it is OK for NoAllocations
   (calls through D::M are assured no allocations will happen, even though calls through
   C::M are guaranteed no such thing).&amp;nbsp; Similarly, we are attempting to remove both
   effects from the overridden N method.&amp;nbsp; Because MayBlock is additive, this is
   OK (M isn’t required to block, even though calls through C::M may suspect it of doing
   so), whereas it is decidedly not OK for NoAllocations (calls through C::M will reasonable
   assume allocations do not happen, whereas D::M would be free to perform them).&amp;nbsp;
   It may take some thought to convince yourself that this is correct, but I hope that
   you find that it is.&amp;nbsp; All of this works because of the subtyping of effect types.
&lt;/p&gt;
&lt;p&gt;
   All of this works similarly with delegates.&amp;nbsp; The source delegate signature is
   akin to the base class in the above example, whereas the target method being bound
   to is like the override.
&lt;/p&gt;
&lt;p&gt;
   Things get a little more complicated when considering the EThrows effect.&amp;nbsp; It
   is additive, so it is of course true that ENil &amp;lt;: EThrows(*).&amp;nbsp; However, what
   if we have two different EThrows, and wish to inquire about substitutability of one
   in place of the other?&amp;nbsp; We can come up with a simple rule that is general purpose
   for all set-of-type kinds of effects.&amp;nbsp; Namely, consider two instances A and B
   of the same effect type:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
   Given an additive effect type EAdditive, then A &amp;lt;: B if, for all types T in A’s
   set-of-types, there exists a type U in B’s set-of-types such that T &amp;lt;: U.
&lt;/p&gt;
&lt;p&gt;
   Given a subtractive effect type ESubtractive, then A &amp;lt;: B if, for all types T in
   A’s set-of-types, there exists a type U in B’s set-of-types such that&amp;nbsp;U &amp;lt;:
   T.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   These sound quite similar, except that they end differently (i.e.&amp;nbsp;T &amp;lt;: U vs.&amp;nbsp;U
   &amp;lt;: T).&amp;nbsp; We may illustrate the additive case with EThrows; to illustrate the
   subtractive case, let us imagine we can declare a ENoAllocations effect type that
   specifies which precise types may not be allocated:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;code&gt; 
&lt;p&gt;
   class A{}&lt;br&gt;
   class B : A{}
&lt;/p&gt;
&lt;p&gt;
   class C&lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; [Throws(typeof(Exception)), NoAllocations(typeof(A))]&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual void M() {}&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; [Throws(typeof(FileNotFoundException)), NoAllocations(typeof(B))]&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual void N() {}&lt;br&gt;
   }
&lt;/p&gt;
&lt;p&gt;
   class D : C&lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; [Throws(typeof(FileNotFoundException)), NoAllocations(typeof(B))]&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public override void M() {}&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; [Throws(typeof(Exception)), NoAllocations(typeof(A))]&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; public override void N() {}&lt;br&gt;
   }
&lt;/p&gt;
&lt;/code&gt;&lt;/blockquote&gt; 
&lt;p&gt;
   The results should not be surprising.&amp;nbsp; D::M overrides C::M’s exception list,
   by being more specific and declaring that FileNotFoundException is thrown instead
   of just Exception.&amp;nbsp; This is OK.&amp;nbsp; Whereas D::N overrides C::N’s list by being
   more general purpose, specifying Exception instead of FileNotFoundException.&amp;nbsp;
   This is clearly not OK.&amp;nbsp; The NoAllocations type works in exactly the reverse.&amp;nbsp;
   D::M attempts to prohibit allocations of B, but this is merely one possible subtype
   of the base method C::M’s declaration of A, and therefore this is illegal.&amp;nbsp; Whereas
   D::N ensures no instances of A are allocated, which of course subsumes the base method
   C::N’s declaration that no B’s are allocated.
&lt;/p&gt;
&lt;p&gt;
   Everything gets a little more interesting when you consider generics.&amp;nbsp; For example,
   how would we type a general purpose Map method?&amp;nbsp; (This pattern arises quite frequently.)&amp;nbsp;
   We would presumably want it to somehow “acquire” the effects of the delegate it invokes
   on all elements in a list.&amp;nbsp; For example:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;code&gt; 
&lt;p&gt;
   U[] Map&amp;lt;T, U&amp;gt;(T[] input, Func&amp;lt;T, U&amp;gt; func);
&lt;/p&gt;
&lt;/code&gt;&lt;/blockquote&gt; 
&lt;p&gt;
   This declaration is stronger than necessary.&amp;nbsp; The Func&amp;lt;T, U&amp;gt; class – prepackaged
   with the .NET Framework – does not have any effects on it.&amp;nbsp; So it may not, for
   example, bind to a method that has any additive effects like Throws on it.&amp;nbsp; This
   is rather unfortunate.
&lt;/p&gt;
&lt;p&gt;
   To solve this we could imagine treating effects with parametric polymorphism:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;code&gt; 
&lt;p&gt;
   [Effects(E)]&lt;br&gt;
   U Func&amp;lt;T, U, [EffectParameter] E&amp;gt;(T x);
&lt;/p&gt;
&lt;/code&gt;&lt;/blockquote&gt; 
&lt;p&gt;
   This fictitious syntax merely says that Func can be instantiated with an effect type
   E, and that the Func “method” itself acquires the effect E.&amp;nbsp; (Admittedly I should
   stop using faux-attribute syntax for illustrations since we’ve reached this level
   of language integration.)&amp;nbsp; Now Map can be declared as such:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;code&gt; 
&lt;p&gt;
   [Effects(E)]&lt;br&gt;
   U[] Map&amp;lt;T, U, [EffectParameter] E&amp;gt;(T[] input, Func&amp;lt;T, U, E&amp;gt; func);
&lt;/p&gt;
&lt;/code&gt;&lt;/blockquote&gt; 
&lt;p&gt;
   This says that Map has the same effects as the Func that is supplied as an argument.&amp;nbsp;
   It turns out that we may want to extend this further, by enabling symbolic manipulation
   of effects.&amp;nbsp; We may wish, for example, to specify that the Func is not allowed
   to block, by stating it does not have [MayBlock] in it.&amp;nbsp; You could imagine using
   something very similar to generic constraints to achieve this.&amp;nbsp; It is also interesting
   to allow concatenation of multiple effect types, both through partial and full specialization.&amp;nbsp;
   For example, Map above may clearly have effects of its own.&amp;nbsp; You also tend to
   want generic constraints like, 'where E : F', which of course just depends on the
   aforementioned subtyping rules.&amp;nbsp; And of course C# 4.0's co- and contravariance
   can be applied to effects too.
&lt;/p&gt;
&lt;p&gt;
   Anyway, I have probably gone beyond most readers’ interest level in this subject.&amp;nbsp;
   Things sure do get very interesting when you allow symbolic manipulation of effects.&amp;nbsp;
   They get even more interesting when you begin to think of types as having “permissible
   effects” attached to them.&amp;nbsp; However, the main thing I wanted to point out with
   this brief article is that this pattern arises quite frequently.&amp;nbsp; And despite
   everyone struggling through what seem to be odd corner cases as they develop ad-hoc
   solutions, there really is a sound generalization behind it all.&amp;nbsp; Many languages
   have first class effect typing, and I have found it liberating to think of many of
   these type system annotations through that lens.&amp;nbsp; Perhaps you shall too.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=ae76cfe5-e205-437a-a2e0-cd91f8b13654" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,ae76cfe5-e205-437a-a2e0-cd91f8b13654.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=14b37ade-3110-4596-9d6e-bacdcd75baa8</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=14b37ade-3110-4596-9d6e-bacdcd75baa8</wfw:commentRss>
      <slash:comments>12</slash:comments>
      <title>On (Haskell) Type Classes and (C#) Interfaces</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/03/01/OnHaskellTypeClassesAndCInterfaces.aspx</link>
      <pubDate>Mon, 01 Mar 2010 04:52:59 GMT</pubDate>
      <description>&lt;p&gt;
   Simon Peyton Jones was in town a couple weeks back to deliver a repeat of his ECOOP’09
   keynote, &lt;a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/haskell-retrospective/"&gt;“Classes,
   Jim, but not as we know them. Type classes in Haskell: what, why, and whither”&lt;/a&gt;,
   to a group of internal Microsoft language folks. It was a fantastic talk, and pulled
   together multiple strains of thought that I’ve been pondering lately, most notably
   the common thread amongst them.
&lt;/p&gt;
&lt;p&gt;
   In the talk, he compared polymorphism in Java-like languages (including C# which I
   will switch to referring to over Java hereforth) with ML and Haskell. In other words,
   how does a programmer commonly write code in each language that is maximally reusable?
   Of course, C# programmers primarily achieve this through subclassing, whereas functional
   programmers rely on type parameterization. Over the years, however, the former group
   has begun to borrow a great deal from the latter; as evidence, witness the growingly-pervasive
   use of generics in both Java and C# over the past decade. The talk was given mainly
   through the lens of this evolution, which appears to approach an interesting limit
   if projected far enough into the future.
&lt;/p&gt;
&lt;p&gt;
   Type classes came on the scene towards the end of the 1980’s, and immediately became
   a fertile seed for research and exploration in the relationship between subclass and
   parametric polymorphism. Type classes are much closer to subclass polymorphism than
   Haskell’s borrowed ML-style, which is to say parametric polymorphism. This is most
   intriguing because Haskell does not rely on subclassing, and so the mixture of two
   breeds new patterns.
&lt;/p&gt;
&lt;p&gt;
   I thought that it might be interesting to compare the mixture of subclass and parametric
   polymorphism in Haskell vis-à-vis type classes with the same in C# vis-à-vis a mixture
   of interfaces, generics, and generic constraints. Hence this post. We shall proceed
   by examining some basic type classes in Haskell with their equals in C#. Though similar,
   the dissimilarities are as stark as the similarities. And the lack of higher kinds
   -- particularly when combined with type classes -- means that some Haskell patterns
   simply are not expressible in C#.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;The Simple Case: Equality (or Lack Thereof)&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   The most basic type class of all is Eq, which allows the comparison of two like-typed
   pieces of data. This may seem like a commodity if you ordinarily write code in languages
   like Java and C# which have a strong notion of object identity. In Haskell, however,
   equality is value equality over algebraic data types rather than objects, so polymorphism
   over equality operators is quite a bit more important. Indeed, as we shall see, Haskell’s
   approach is more powerful than == in Java-like languages. (Witness the neverending
   dichotomy between reference and value equality vis-à-vis Object.Equals in C#.) But
   alas, let us proceed by crawling in a series of logical steps, rather than leaping
   to the conclusions.
&lt;/p&gt;
&lt;p&gt;
   Haskell’s Eq type class is defined as such:
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; class Eq a where&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; (==), (/=) :: a -&gt; a -&gt; Bool&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;
   &lt;o:p&gt;
      &lt;font color=#0f243e&gt;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; x /= y = not (x == y)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; x == y = not (x /= y)&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   As you see, Eq provides two operators: == and /=. Default implementations of each
   define == as the inverse of /= and /= as the inverse of ==. Not only is this a convenience,
   but it also specifies the desired contract implementations ought to abide by. Other
   types may become members of the Eq class by mapping the one or both operators to type-specific
   functionality. You will immediately recognize the similarity to virtual methods in
   OOP languages, where the operators can be overridden by subclasses.
&lt;/p&gt;
&lt;p&gt;
   Of course all of the primitive data types already implement Eq, so you get value equality
   over numbers, strings, etc. Imagine we declared a new Coords type – comprised of two
   integers – and want to make it a member of Eq also – wherein equality is determined
   by a pairwise comparison of each’s members:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; data Coords = Coords { fst
   :: Integer, snd :: Integer }&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
   We make Coords a member of the Eq type class, and thereby define equality over instance,
   through the ‘instance Eq Coords where’ construct. This maps type class functions to
   real implementation functions. The example here defines them inline, though you may
   of course refer to existing functions instead:
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; instance Eq Coords where&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; (Coords fst1 snd1) == (Coords
   fst2 snd2) = (fst1 == fs2) &amp;&amp; (snd1 == snd2)&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   Now we can take a ‘[Coords]’ and ask whether a particular ‘Coords’ exists within it.
&lt;/p&gt;
&lt;p&gt;
   A function may constrain a type variable to a certain class, and thereby access members
   of that class. For example, the following ‘isin’ function tests whether an instance
   of some type ‘a’ is contained within a list of type ‘[a]’. To do this, it demands
   that ‘a’ is a member of Eq using the syntax “Eq a =&gt;”:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; isin :: Eq a =&gt; a -&gt; [a]
   -&gt; Bool&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; x `isin` [] = False&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; x `isin` (y:ys) = x == y
   || (x `isin` ys)&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   The moral equivalent to the Eq type class in C# is not so easy to decide. The most
   obvious first guess is 
   &lt;br&gt;
   the built-in == and != operators. However, we will quickly find that this is not quite
   right, because these operators are not polymorphic in C#. To illustrate this point,
   let’s try to write the ‘isin’ method in C#, using generics and the == operator, for
   example:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T y in ys) {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;if
   (x == y)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   true;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This function will not compile. The reason is that == and != in C# are not defined
   over all types (specifically not for value types). You can get IsIn to compile by
   restricting the T to a reference type:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys) where T : class&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;…
   same as above …&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   Although this code is deceptively similar to the Haskell example, it is actually quite
   different. The == used to compare two instances compiles into the MSIL CEQ operator,
   effectively hard-coding an object identity comparison. Even if an overloaded == operator
   for a particular instantiated T is available, the compiler will not bind to it. Why?
   Because it is overloading and specifically *not* overriding. For example, say we had
   a MyData type and an overloaded == operator for comparing two instances:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;class
   MyClass&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static bool operator ==(MyClass a, MyClass b) { return true; }&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static bool operator !=(MyClass a, MyClass b) { return false; }&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   According to this, all MyClass objects are equal. However, the following call yields
   the answer ‘false’:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;IsIn&amp;lt;MyClass&amp;gt;(new
   MyClass(), new MyClass[] { new MyClass() });&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
   The same problem arises should instances of MyClass get referred to by Object references.
   == and != do not perform any kind of virtual dispatch; the selection of implementation
   is chosen statically.
&lt;/p&gt;
&lt;p&gt;
   Perhaps it is the Equals method inherited from System.Object, then? This, at least,
   is virtual. And indeed, this gets much closer to Eq. Any type may override Equals,
   and a generic definition defined in terms of it dispatches virtually and allows subclasses
   to change behavior on a type-by-type basis:
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T y in ys) {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;if
   (x == y || (x != null &amp;&amp; x.Equals(y)))&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   true;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   false;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   (Even this is slightly different, because it assumes a certain type-agnostic behavior
   about nulls.)
&lt;/p&gt;
&lt;p&gt;
   This is cheating, however. We’ve taken advantage of the fact that someone thought
   to put an Equals method on System.Object, thereby giving all Ts such a method. There
   are clearly limits to how many crosscutting things can be added to System.Object before
   it becomes overwhelmed with concepts, not to mention the size (e.g. v-tables). Moreover,
   Equals on Object is weakly typed; a better solution is to use interfaces, like the
   IEquatable&amp;lt;T&amp;gt; interface that introduces a strongly typed Equals method:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   interface IEquatable&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   Equals(T other);&lt;br&gt;
   &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   And to use a generic type constraint on IsIn’s T, much more akin to what ‘isin’ in
   Haskell above did:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where
   T : IEquatable&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T y in ys) {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;if
   (x == y || (x != null &amp;&amp; x.Equals(y)))&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   true;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; return false;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This is cheating a little less, because we can implement an interface after-the-fact
   without impacting a class’s type hierarchy. This, in fact, looks remarkably similar
   to the Haskell ‘isin’ shown earlier, using type classes and parametric polymorphism,
   where here we have used interfaces in place of type classes.
&lt;/p&gt;
&lt;p&gt;
   We might be tempted to define a default NotEquals method over all IEquatable&amp;lt;T&amp;gt;
   instances, just like Haskell does by implementing the defaults for == and /= as the
   inverse of each other:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static class Equatable&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static bool NotEquals&amp;lt;T&amp;gt;(this IEquatable&amp;lt;T&amp;gt; @this, IEquatable&amp;lt;T&amp;gt;
   other)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   !this.Equals(other);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This is not perfect. It is not polymorphic; see my previous post for &lt;a href="http://www.bluebytesoftware.com/blog/2010/02/10/ExtensionMethodsAsDefaultInterfaceMethodImplementations.aspx"&gt;an
   extensive discussion&lt;/a&gt; of this and related points. And what about nulls? If '@this'
   is null, the default implementation is going to AV. We’d need to bake in type-agnostic
   knowledge of null again. Sigh!
&lt;/p&gt;
&lt;p&gt;
   Sadly, it turns out this whole approach in general isn’t quite right anyway. For two
   reasons:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      First, we still infect the type in question with the interface being implemented;
      it cannot be done completely outside of the type’s definition, as with type classes.&lt;/li&gt;
   &lt;li&gt;
      Second, type classes in Haskell do not actually require a value of the type in question
      to dispatch against the class’s functions, whereas we clearly do in the above example:
      we need to virtually dispatch against the object, and rely on this virtual dispatch
      to execute different code for each type. This will come up as we look at the numeric
      classes, but it is a critical difference.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   A closer analogy is to use IEqualityComparer&amp;lt;T&amp;gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; public interface IEqualityComparer&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; bool Equals(T x, T y);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; }&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   (IEqualityComparer&amp;lt;T&amp;gt; in .NET also has a GetHashCode method on it. Let’s ignore
   that for now.)
&lt;/p&gt;
&lt;p&gt;
   Unfortunately, if our IsIn method were to use IEqualityComparer&amp;lt;T&amp;gt; to do its
   job, callers would be required to pass an instance explicitly; we cannot infer a “default”
   comparer based solely on the T:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;bool
   IsIn&amp;lt;T&amp;gt;(T x, T[] ys, IEqualityComparer&amp;lt;T&amp;gt; eq)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T y in ys) {&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;if
   (eq.Equals(x, y))&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   true;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   false;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   Type classes actually function rather similarly, with two major differences:
&lt;/p&gt;
&lt;ol&gt;
   &lt;li&gt;
      The interface object – called a dictionary – is passed and used implicitly.&lt;/li&gt;
   &lt;li&gt;
      The mapping from types to dictionaries is done implicitly, whereas in .NET you’ll
      need to find an instance of the interface in question through other means.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
   This second difference is solved by a little hack in .NET. If you take a look at the
   EqualityComparer&amp;lt;T&amp;gt;.Default property, you shall see a lot of slightly gross
   reflection code to return an instance of IEqualityComparer&amp;lt;T&amp;gt; for any arbitrary
   T. The code checks some well-known types and conditions, and ultimately falls back
   to the aforementioned interfaces and default Equals method for the most general case.
   It’s not pretty, but it’s a beautiful hack given the tools at our disposal in C#.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;A Harder Case: Polymorphic Numbers, on Output Parameters&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   The Eq type class is easy. The functions it defines are polymorphic on their inputs,
   but not on their outputs; both == and /= return Bool values. Once we transition to
   polymorphic output parameters or return values, we encounter a pattern quite different
   from that which is found in most .NET interfaces.
&lt;/p&gt;
&lt;p&gt;
   Let’s illustrate these differences by looking at Haskell’s Num type class:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;class&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;(Eq
   a, Show a) =&gt; Num a&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;(+),
   (-), (*)&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;:: a -&gt; a -&gt; a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;negate&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   a -&gt; a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;abs,
   signum&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;:: a -&gt; a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;fromInteger&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   Integer -&gt; a&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   Here we see another feature of Haskell type classes: inheritance. Num derives from
   both Eq and Show – indicated by “(Eq a, Show a) =&gt; Num a” – the latter class of which
   we have not yet shown but is the moral equivalent to .NET’s Object.ToString method.
   It enables pretty printing of values, clearly something that would be expected to
   be common among all numeric data types. Haskell’s numeric class hierarchy is quite
   elegant, enabling highly polymorphic computations. A nice little tutorial of can be
   found here: &lt;a href="http://www.haskell.org/tutorial/numbers.html"&gt;http://www.haskell.org/tutorial/numbers.html&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   But the question at hand is what the C# equivalent would be.
&lt;/p&gt;
&lt;p&gt;
   Our first approach would be to mimic the IEquatable&amp;lt;T&amp;gt; solution above:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;interface
   INumeric&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Add(T d);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Subtract(T d);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Multiply(T d);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Absolute();&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   FromInteger(int x);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This works fine, and primitive types in .NET could presumably implement it:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;struct
   int : INumeric&lt;int&gt;
      &lt;INT&gt;
         { .. }
   &lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;struct
   float : INumeric&lt;float&gt;
      &lt;FLOAT&gt;
         { .. }
   &lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;struct
   double : INumeric&lt;double&gt;
      &lt;DOUBLE&gt;
         { .. }
   &lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;…&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This enables polymorphic code, like a Sum method, through the use of generic type
   constraints:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static T Sum&amp;lt;T&amp;gt;(params T[] values)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where
   T : INumeric&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;T
   accum = default(T);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;foreach
   (T v in values)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;accum
   = v.Add(accum);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   accum;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This example works great. Why then, you might wonder, doesn’t LINQ use this instead
   of providing special-case overloads of Average, Min, Max, Sum, etc. for all well-known
   primitive data types?
&lt;/p&gt;
&lt;p&gt;
   The primary reason is the performance hit taken to perform addition through O(N) interface
   calls versus O(N) MSIL ADD instructions. It is just a basic fact of life that today’s
   leading edge separate compilation techniques will not achieve parity with the hand
   specialized variants. While it is true that the JIT compiler *could* specialize the
   code for specific Ts and specific interfaces to emit more efficient instructions,
   like int, float, etc. over INumeric&amp;lt;T&amp;gt; calls, it will not do so today. This
   reduces the ability to share code – which admittedly is what we want here – and is
   tangled up in a judgment call based on heuristics. But I digress.
&lt;/p&gt;
&lt;p&gt;
   There is a larger problem that arises with other examples, at least from a language
   expressiveness point-of-view: the need to have an instance in hand to invoke interface
   methods. FromInteger, for example, is rather awkward to write. In fact, we cannot
   write a method with INumeric&amp;lt;T&amp;gt; like we could in Haskell:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static T MakeT&amp;lt;T&amp;gt;(int value)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where
   T : INumeric&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;…
   ? …&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   How do we invoke FromInteger, given that no T is available at the time of MakeT’s
   invocation? You can’t; you need to arrange for an instance to be available. There
   are ways out of this corner. One solution is to mandate that T has a default constructor:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   static T MakeT&amp;lt;T&amp;gt;(int value)&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where
   T : INumeric&amp;lt;T&amp;gt;, new()&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return
   new T(value).FromInteger(value);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   That is always acceptable for structs, since they always have such a constructor;
   but this practice requires that classes be designed to possibly not hold invariants
   at all times, and so is not always acceptable or at the very least requires design
   accommodation.
&lt;/p&gt;
&lt;p&gt;
   The alternative is probably obvious. Use a similar approach to IEqualityComparer&amp;lt;T&amp;gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;interface
   INumericProvider&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Add(T x, T y);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Subtract(T x, T y);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Multiply(T x, T y);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   Absolute(T x);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; &lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt; T
   FromInteger(int x);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   And now, of course, each method that does polymorphic number crunching must accept
   an instance of INumericProvider&amp;lt;T&amp;gt;. That’s particularly cumbersome, so it’s
   more likely that .NET developers would prefer the aforementioned approach, where the
   type must provide a default constructor.
&lt;/p&gt;
&lt;p&gt;
   Admittedly, I seldom run into this particular problem in practice; but when I do,
   I really wish I had something like Haskell type classes to help me out.
&lt;/p&gt;
&lt;p&gt;
   Before moving on, it is worth pointing out one Haskell type class problem that explicit
   interface object passing in .NET helps to avoid. Should you need multiple implementations
   of a given class for the same type, as is relatively common with equality comparisons,
   you must disambiguate in Haskell by separation by module and being careful about what
   you import. This is similar to C#’s extension methods. With explicitly passed interface
   objects, however, it is trivial to manage and pass separate objects if you’d like.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Close, but No Cigar: Higher Kinds&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   There is one last feature that Haskell provides – a pretty big one, I might add –
   that C# simply cannot do: higher kinded types, or polymorphism over constructed types.
   This feature is orthogonal to type classes, but gets used pervasively in conjunction
   with them. An example will make this stunningly clear:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;class&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;Monad
   m&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;where&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;(&gt;&gt;=)&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   m a -&gt; (a -&gt; m b) -&gt; m b&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;(&gt;&gt;)&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   m a -&gt; m b -&gt; m b&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;return&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   a -&gt; m a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;fail&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;::
   String -&gt; m a&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#0f243e&gt;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;m
   &gt;&gt; k&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;=&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;m
   &gt;&gt;= \_ -&gt; k&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;fail
   s&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;= error s&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
   Let’s try to transcribe the core of this class in C#, renaming &gt;&gt;= to Bind, and omitting
   the &gt;&gt; and ‘fail s’ operators because they have default implementations:
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   interface IMonad&amp;lt;M, A&amp;gt;&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;A, M&amp;lt;B&amp;gt;&gt;M&amp;lt;B&amp;gt;
   Bind&amp;lt;B&amp;gt;(M&amp;lt;A&amp;gt; m, Func k);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;M&amp;lt;A&amp;gt;
   Return(A a);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;M&amp;lt;A&amp;gt;
   Fail(string s);&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   This approach is tantalizingly close. It suffers from the already-admitted problem
   that, for any M&amp;lt;A&amp;gt; instance, you will need to pass the appropriate IMonad&amp;lt;A&amp;gt;
   provider object – just as with the IEqualityComparer&amp;lt;T&amp;gt; and INumericProvider&amp;lt;T&amp;gt;
   examples above.
&lt;/p&gt;
&lt;p&gt;
   But the code of course won’t *actually* compile, because the type variable M cannot
   be constructed as shown here. We find references to M&amp;lt;A&amp;gt; and M&amp;lt;B&amp;gt;, which
   are complete nonsense to C#. M is just a plain type variable. M is required to be
   what Haskell calls a type constructor (* -&gt; *), which is a generic type that must
   be instantiated before it is a terminal type. &lt;a href="http://www.bluebytesoftware.com/blog/2008/11/04/LongingForHigherkindedC.aspx"&gt;I’ve
   written about this before&lt;/a&gt;. Although it seems like a trivial omission in C#’s language
   definition, it strikes at the heart of the type system.
&lt;/p&gt;
&lt;p&gt;
   A fictitious syntax for expressing this in C# might be:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;public
   interface IMonad&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;&lt;&gt;&lt;/FONT&gt;where
   M : 
   &lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;{&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt; ...&lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   And if, say, M were expected to be a two- or three-parametered type, we would find,
   respectively:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; ... &lt;/span&gt;&lt;,&gt;where
   M : &lt;/font&gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-FAMILY: Consolas"&gt;&lt;font color=#0f243e&gt;&lt;span style="mso-spacerun: yes"&gt; ... &lt;/span&gt;&lt;,,&gt;where
   M : &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;p&gt;
   And so on.
&lt;/p&gt;
&lt;p&gt;
   This could in theory work. But C# -- and more worrisome .NET and the CLR – do not
   support this presently, and, to be quite honest, likely never will. It is immensely
   powerful, however. Life without monads is a life destined to continuous repetition.
   The “LINQ Pattern”, for example, is one example case in .NET where, for each ‘source’
   type, we must create a “copy” of the original System.Linq.Enumerable variant. And
   shame on those who wish to write polymorphic code that will work for any LINQ provider.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Winding Down&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Let’s wind down. I need to go grab dinner at Mama's Fish House on Maui right now.
&lt;/p&gt;
&lt;p&gt;
   I hope to have shown some of the similarities and dissimilarities between type classes
   and interfaces, and some patterns that arise when these things are mixed with parametric
   polymorphism. The mix of inheritance for type classes, but not for implementation
   types, in Haskell is unique. C#, of course, allows inheritance both amongst interfaces
   and implementations which is both a blessing and a curse.
&lt;/p&gt;
&lt;p&gt;
   I do think both camps have something to teach one another. For example, having a default
   interface lookup mechanism for arbitrary types in C# would be wonderful, and indeed
   might provide a replacement for extension methods that has more longevity. I’m sure
   much of this will happen with time; either “in place” as the respective languages
   evolve, or as new languages are created with time. 
&lt;/p&gt;
&lt;p&gt;
   But most importantly, I hope that the blog post was educational and fun. Enjoy.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=14b37ade-3110-4596-9d6e-bacdcd75baa8" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,14b37ade-3110-4596-9d6e-bacdcd75baa8.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=b9072376-beaf-4d17-a6c4-4bfccfbd34b0</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,b9072376-beaf-4d17-a6c4-4bfccfbd34b0.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,b9072376-beaf-4d17-a6c4-4bfccfbd34b0.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=b9072376-beaf-4d17-a6c4-4bfccfbd34b0</wfw:commentRss>
      <slash:comments>7</slash:comments>
      <title>Extension methods as default interface method implementations</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,b9072376-beaf-4d17-a6c4-4bfccfbd34b0.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/02/10/ExtensionMethodsAsDefaultInterfaceMethodImplementations.aspx</link>
      <pubDate>Wed, 10 Feb 2010 02:21:49 GMT</pubDate>
      <description>&lt;p&gt;
   One of my comments in the 2nd edition of the .NET Framework design guidelines (on
   page 164) was that you can use extension methods as a way of getting default implementations
   for interface methods.&amp;nbsp; We've actually begun using these techniques here on my
   team.&amp;nbsp; To illustrate this trick, let's rewind the clock and imagine we were designing
   new collections APIs from day one.
&lt;/p&gt;
&lt;p&gt;
   Let's say we gave the core interfaces the most general methods possible.&amp;nbsp; These
   may neither be the most user friendly overloads nor the ones that most people use
   all the time.&amp;nbsp; They would, however, be those from which all the other convenience
   methods could be implemented.&amp;nbsp; An INewList&amp;lt;T&amp;gt; interface that was designed
   with these principles in mind may look like this:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;public
   interface INewList&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   Count { get; }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;T
   this[int index] { get; set; }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   InsertAt(int index, T item);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   RemoveAt(int index);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   This interface is missing all the nice convenience methods you will find on .NET's
   IList&amp;lt;T&amp;gt;, like Add, Clear, Contains, CopyTo, IndexOf, and Remove.&amp;nbsp; So it's
   not really as nice to use.&amp;nbsp; You can't write an API that takes in an INewList&amp;lt;T&amp;gt;
   and performs an Add against it, for example, like you can with IList&amp;lt;T&amp;gt;.
&lt;/p&gt;
&lt;p&gt;
   One approach to solving this might be to write a concrete class -- much like .NET's
   System.Collections.ObjectModel.Collection&amp;lt;T&amp;gt; -- that provides concrete implementations
   of all of these methods, and then other lists can simply subclass that.&amp;nbsp; But
   we can do better.
&lt;/p&gt;
&lt;p&gt;
   Instead, let's give INewList&amp;lt;T&amp;gt; default implementations of all of these methods.&amp;nbsp;
   How do we do this?&amp;nbsp; That's right: with extension methods.&amp;nbsp; Voila!
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public
   static class NewListExtensions&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Add&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lst.InsertAt(lst.Count,
   item);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Clear&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   count;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   ((count = lst.Count) &amp;gt; 0) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lst.RemoveAt(count
   - 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static bool Contains&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   lst.IndexOf(item) != -1;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void CopyTo&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T[] array, int arrayIndex)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; lst.Count; i++) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;array[arrayIndex
   + i] = lst[i];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static int IndexOf&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
   eq = EqualityComparer&amp;lt;T&amp;gt;.Default;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; lst.Count; i++) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (eq.Equals(item, lst[i])) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   i;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   -1;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static bool Remove&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst, T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   index = lst.IndexOf(item);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (index == -1) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   false;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lst.RemoveAt(index);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   true;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Well isn't that neat.&amp;nbsp; We've now given any INewList&amp;lt;T&amp;gt; implementations
   all these common methods without dirtying their class hierarchies, built atop a tiny
   core of&amp;nbsp;extensibility.&amp;nbsp; This is much like .NET's Collection&amp;lt;T&amp;gt; which
   exposes the core as abstract methods.&amp;nbsp; Indeed, we can go even further.&amp;nbsp;
   Any convenience overloads, like the multitude of CopyTos on List&amp;lt;T&amp;gt; in .NET,
   can be given to all INewList&amp;lt;T&amp;gt;'s also.&amp;nbsp; And yet implementing INewList&amp;lt;T&amp;gt;
   remains as braindead simple as it was before: two properties and two methods.&amp;nbsp;
   In fact, it's simpler than doing a more feature-rich IList&amp;lt;T&amp;gt;, because the convenience
   methods come for free.
&lt;/p&gt;
&lt;p&gt;
   It would be even niftier if you could add these methods straight onto INewList&amp;lt;T&amp;gt;,
   and have the C# compiler emit the extension methods silently for you.&amp;nbsp; In other
   words:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public
   interface INewList&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...
   interface methods (as above) ...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   Add(T item)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;InsertAt(Count,
   item);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   Clear()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   count;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   ((count = Count) &amp;gt; 0) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;RemoveAt(count
   - 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...
   and so on ...&lt;br&gt;
   &lt;/font&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;font color=#000000&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;/span&gt;Although
   this would just be sugar for the&amp;nbsp;NewListExtensions class shown earlier, it sure
   saves some typing and makes it the pattern more apparent and first class.
&lt;/p&gt;
&lt;p&gt;
   Though cool, this whole idea is certainly not perfect.
&lt;/p&gt;
&lt;p&gt;
   For one, there are no extension properties.&amp;nbsp; So you can't use this trick for
   properties.
&lt;/p&gt;
&lt;p&gt;
   But the more obvious and severe downside to this approach that these methods are not
   specialized for the given concrete type.&amp;nbsp; For example, the Clear method is potentially
   far less efficient than a hand-rolled List&amp;lt;T&amp;gt;, because it does O(N) RemoveAts
   rather than a single O(1) fixup of the count.
&lt;/p&gt;
&lt;p&gt;
   Recall now that the compiler binds more tightly to instance methods than extension
   methods.&amp;nbsp; So we could implement our own little list class with a faster Clear
   method if we'd like:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;class
   MyList : INewList&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...
   the two properties and two methods from INewList&amp;lt;T&amp;gt; ...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   void Clear()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;..
   efficient! ...&lt;br&gt;
   &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Now when someone calls Clear on a MyList&amp;lt;T&amp;gt; directly, the compiler will bind
   to the efficient Clear.
&lt;/p&gt;
&lt;p&gt;
   This is still not perfect.&amp;nbsp; If you pass the MyList&amp;lt;T&amp;gt; to an API that takes
   in an INewList&amp;lt;T&amp;gt;, any calls to Clear will fall back to the extension method.&amp;nbsp;
   Extension methods are not virtual in any way.&amp;nbsp; You can try to simulate virtual
   dispatch, but it gets messy quick.&amp;nbsp; For example, say we defined an IFasterList&amp;lt;T&amp;gt;
   that includes all those convenience methods that lists frequently want to make faster;
   we can then do a typecheck plus virtual dispatch in the extension method.
&lt;/p&gt;
&lt;p&gt;
   For now, let's pretend that's just the Clear method:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;public
   interface IFasterList&amp;lt;T&amp;gt; : INewList&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   Clear();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Of course, MyList&amp;lt;T&amp;gt; above would now implement IFasterList&amp;lt;T&amp;gt;.&amp;nbsp; Invocations
   through IFasterList&amp;lt;T&amp;gt; will automatically bind to the faster variant; but if
   objects that implement IFasterList&amp;lt;T&amp;gt; get passed around as IList&amp;lt;T&amp;gt;s,
   you lose this ability.&amp;nbsp; So the Clear extension method can now do a typecheck:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Clear&amp;lt;T&amp;gt;(this INewList&amp;lt;T&amp;gt; lst)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;IFasterList&amp;lt;T&amp;gt;
   fstLst = lst as IFasterList&amp;lt;T&amp;gt;;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;if
   (fstLst != null) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;fstLst.Clear();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   count;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   ((count = lst.Count) &amp;gt; 0) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lst.RemoveAt(count
   - 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   This works but is obviously a tedious and hard-to-maintain solution.&amp;nbsp; It would
   be neat if someday C# figured out a way to "magically" reconcile virtual dispatch
   and extension methods.&amp;nbsp; I don't know if there is a clever solution out there.&amp;nbsp;
   I am skeptical.&amp;nbsp; Nevertheless, despite this flaw, the above techniques are certainly
   thought provoking and interesting enough to play around with and consider for your
   own projects.&amp;nbsp; And at the very least, it's fun.&amp;nbsp; Enjoy.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=b9072376-beaf-4d17-a6c4-4bfccfbd34b0" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,b9072376-beaf-4d17-a6c4-4bfccfbd34b0.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=53e892af-d387-4283-9b10-a463f8ac3eba</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,53e892af-d387-4283-9b10-a463f8ac3eba.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,53e892af-d387-4283-9b10-a463f8ac3eba.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=53e892af-d387-4283-9b10-a463f8ac3eba</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      Sometimes you need to wait for something before proceeding with a computation.
   </p>
        <p>
      Perhaps you need to know the value of some integer that is being computed concurrently. 
      Maybe you need to wait for the bytes to flush to disk before telling another process
      the file is consistent and ready to read.  Or you need to get that next row back
      from the database before painting it on the UI.  It could be that you need to
      wait for the missile to leave the bay before closing the bay door.  And so on.
   </p>
        <p>
      And sometimes there’s simply nothing better to do while waiting for these things to
      happen other than to let the CPU halt (or let other processes on the machine run). 
      You need to twiddle your thumbs a bit, and exhibit a little patience.  Or at
      least your program does.  This is simply an unfortunate fact of life.
   </p>
        <p>
      This manifests numerous ways in our programming models:
   </p>
        <p>
              1) Waiting on an event.<br />
              2) Waiting to acquire an already-held
      lock.<br />
              3) Finding that the GUI message queue
      is empty and doing a MsgWaitForMultipleObjectsEx.<br />
              4) Finding that the COM RPC queue
      is empty and doing a CoWaitForMultipleHandles.<br />
              5) Issuing an Ada rendezvous ‘accept’
      and finding that no messages await you, thus blocking.<br />
              6) Issuing an Erlang ‘receive’ and
      finding that no messages await you, thus blocking.<br />
              7) Waiting on a .NET 4.0 task.<br />
              8) Issuing a ContinueWith on a .NET
      4.0 task.<br />
              9) And so on.
   </p>
        <p>
      There are three big distinctions to make about the characteristic nature
      of this waiting: namely, (1) what condition's establishment is being sought --
      i.e. the reason for the wait, (2) whether multiple such conditions of interest
      may be waited on simultaneously, and, related, (3) whether waiting for said condition(s)
      necessarily means that the processing of some other conditions that may arise elsewhere,
      but require the blocked context to run, cannot occur.
   </p>
        <p>
      I will be the first to admit that this statement is rather abstract.  But it
      really does matter.
   </p>
        <p>
      For example, MsgWaitForMultipleObjectsEx is a pumping wait.  Not only do you
      wait for the occurrence of one of several events to get set, but the arrival
      of a new top-level message at the message queue (either GUI or COM RPC-related) causes
      immediate processing of that message, presuming the thread is blocked at that call
      at the time.  Although you can be deeply nested in some complicated code, you
      “jump” to the event loop to run the message handling code.  Vanilla WaitForMultipleObjectsEx
      works in a similar way vis-à-vis APCs, provided the wait is alertable.  This
      is quite different from a fully blocking non-pumping wait, which only waits for one
      or more very specific events, but does not dispatch messages simultaneously.
   </p>
        <p>
      Win32 esoterica aside, the concepts appear elsewhere.  The moral equivalent
      in Ada or Erlang is to do a selective-accept or -receive, intentionally not dispatching
      certain messages that might arrive in the meantime.  (To be fair, you can also
      do this in COM with message filters.)  This often happens when you nest accepts
      and receives.  You may be capable of processing messages A-Z at the top-level
      tail recursive loop; but if that nested accept only knows about message kinds M and
      N, then there are 24 other kinds that will not be picked up in the meantime.
   </p>
        <p>
      Not pumping for messages is dangerous.  And it can lead to deadlock if you pump
      for the wrong ones.  Like if you’re accepting M or N, yet the triggering of M
      or N depends on first processing some message K waiting in the queue.  COM RPCs
      with cycles run face first into this.  And/or not pumping can lead to responsiveness
      and scalability problems.  Perhaps M or N eventually does arrive, yet little
      old K needs to wait an indeterminate amount of time before it is seen.  Whereas
      we could have overlapped its processing.  This is why most STAs pump while waiting,
      and, similarly, why many Erlang processes consist of a main loop that is prepared
      to handle any message the process accepts at that top level loop.  They may seem
      very different but they are strikingly not.
   </p>
        <p>
      Yet paradoxically pumping for messages is also dangerous.  You must predict all
      the kinds of messages that may reentrantly get executed, and your state at the point
      of the blocking call must be consistent enough to tolerate them.  (At least those
      that will actually happen.)  In COM STAs, this can be wholly unpredictable and
      indeed because the CLR auto-pumps on STAs the blocking points can be hidden. 
      Overly aggressively admitting messages may seem like the right thing to do, until
      you’ve wedged yourself into some unforeseen inconsistent state.  You can avoid
      this by making each message handler atomic; see Argus.  But if you can't or don't
      have the discipline to do that, or aren't quite sure, you must not pump.  You
      either avoid pumping altogether or you selectively pump messages that do not
      touch the state encapsulated by the pump.  Or you lock access to state with a
      non-recursive lock and run the risk of deadlock.
   </p>
        <p>
      I have found it clarifying to think about blocking in event loop concurrency
      and state machine terms, advancing from one state to the next in between waits. 
      It’s a slippery model, but particularly when working in message passing systems that
      employ event loops, it can help to identify all the familiar problems with shared
      memory, blocking, and consistency.
   </p>
        <p>
      Indeed it is interesting how blocking and non-blocking systems can rapidly approach
      each other.  Starting from either extreme tempts you to tiptoe closer and closer
      to the middle.  The familiarity of the other extreme tempts you.  Until,
      alas, you just might meet in the middle.
   </p>
        <img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=53e892af-d387-4283-9b10-a463f8ac3eba" />
      </body>
      <title>Musing on messages and blocking</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,53e892af-d387-4283-9b10-a463f8ac3eba.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/01/08/MusingOnMessagesAndBlocking.aspx</link>
      <pubDate>Fri, 08 Jan 2010 08:11:39 GMT</pubDate>
      <description>&lt;p&gt;
   Sometimes you need to wait for something before proceeding with a computation.
&lt;/p&gt;
&lt;p&gt;
   Perhaps you need to know the value of some integer that is being computed concurrently.&amp;nbsp;
   Maybe you need to wait for the bytes to flush to disk before telling another process
   the file is consistent and ready to read.&amp;nbsp; Or you need to get that next row back
   from the database before painting it on the UI.&amp;nbsp; It could be that you need to
   wait for the missile to leave the bay before closing the bay door.&amp;nbsp; And so on.
&lt;/p&gt;
&lt;p&gt;
   And sometimes there’s simply nothing better to do while waiting for these things to
   happen other than to let the CPU halt (or let other processes on the machine run).&amp;nbsp;
   You need to twiddle your thumbs a bit, and exhibit a little patience.&amp;nbsp; Or at
   least your program does.&amp;nbsp; This is simply an unfortunate fact of life.
&lt;/p&gt;
&lt;p&gt;
   This manifests numerous&amp;nbsp;ways in our programming models:
&lt;/p&gt;
&lt;p&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1)&amp;nbsp;Waiting on an event.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2)&amp;nbsp;Waiting to acquire an already-held
   lock.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3)&amp;nbsp;Finding that the GUI message queue
   is empty and doing a MsgWaitForMultipleObjectsEx.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4)&amp;nbsp;Finding that the COM RPC queue
   is empty and doing a CoWaitForMultipleHandles.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5)&amp;nbsp;Issuing an Ada rendezvous ‘accept’
   and finding that no messages await you, thus blocking.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6)&amp;nbsp;Issuing an Erlang ‘receive’ and
   finding that no messages await you, thus blocking.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7)&amp;nbsp;Waiting on a .NET 4.0 task.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8)&amp;nbsp;Issuing a ContinueWith on a .NET
   4.0 task.&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9)&amp;nbsp;And so on.
&lt;/p&gt;
&lt;p&gt;
   There&amp;nbsp;are three big&amp;nbsp;distinctions to make about the characteristic nature
   of this waiting: namely, (1) what condition's establishment&amp;nbsp;is being sought --
   i.e. the reason for the wait, (2) whether&amp;nbsp;multiple such conditions of interest
   may be waited on simultaneously, and, related, (3) whether waiting for said condition(s)
   necessarily means that the processing of some other conditions that may arise elsewhere,
   but require the blocked context to run,&amp;nbsp;cannot occur.
&lt;/p&gt;
&lt;p&gt;
   I will be the first to admit that this statement is rather abstract.&amp;nbsp; But it
   really does&amp;nbsp;matter.
&lt;/p&gt;
&lt;p&gt;
   For example, MsgWaitForMultipleObjectsEx is a pumping wait.&amp;nbsp; Not only do you
   wait for the occurrence of one of several events to get set, but&amp;nbsp;the arrival
   of a new top-level message at the message queue (either GUI or COM RPC-related) causes
   immediate processing of that message, presuming the thread is blocked at that call
   at the time.&amp;nbsp; Although you can be deeply nested in some complicated code, you
   “jump” to the event loop to run the message handling code.&amp;nbsp; Vanilla WaitForMultipleObjectsEx
   works in a similar way vis-à-vis APCs, provided the wait is alertable.&amp;nbsp; This
   is quite different from a fully blocking non-pumping wait, which only waits for one
   or more very specific events, but does not dispatch messages simultaneously.
&lt;/p&gt;
&lt;p&gt;
   Win32 esoterica aside, the concepts appear elsewhere.&amp;nbsp;&amp;nbsp;The moral equivalent
   in Ada or Erlang is to do a selective-accept or -receive, intentionally not dispatching
   certain messages that might arrive in the meantime.&amp;nbsp; (To be fair, you can also
   do this in COM with message filters.)&amp;nbsp; This often happens when you nest accepts
   and receives.&amp;nbsp; You may be capable of processing messages A-Z at the top-level
   tail recursive loop; but if that nested accept only knows about message kinds M and
   N, then there are 24 other kinds that will not be picked up in the meantime.
&lt;/p&gt;
&lt;p&gt;
   Not pumping for messages is dangerous.&amp;nbsp; And it can lead to deadlock if you pump
   for the wrong ones.&amp;nbsp; Like if you’re accepting M or N, yet the triggering of M
   or N depends on first processing some message K waiting in the queue.&amp;nbsp; COM RPCs
   with cycles run face first into this.&amp;nbsp; And/or not pumping can lead to responsiveness
   and scalability problems.&amp;nbsp; Perhaps M or N eventually does arrive, yet little
   old K needs to wait an indeterminate amount of time before it is seen.&amp;nbsp; Whereas
   we could have overlapped its processing.&amp;nbsp; This is why most STAs pump while waiting,
   and, similarly, why many Erlang processes consist of a main loop that is prepared
   to handle any message the process accepts at that top level loop.&amp;nbsp; They may seem
   very different but they are strikingly not.
&lt;/p&gt;
&lt;p&gt;
   Yet paradoxically pumping for messages is also dangerous.&amp;nbsp; You must predict all
   the kinds of messages that may reentrantly get executed, and your state at the point
   of the blocking call must be consistent enough to tolerate them.&amp;nbsp; (At least those
   that will actually happen.)&amp;nbsp; In COM STAs, this can be wholly unpredictable and
   indeed because the CLR auto-pumps on STAs the blocking points can be hidden.&amp;nbsp;
   Overly aggressively admitting messages may seem like the right thing to do, until
   you’ve wedged yourself into some unforeseen inconsistent state.&amp;nbsp; You can avoid
   this by making each message handler atomic; see Argus.&amp;nbsp; But if you can't or don't
   have the discipline to do that, or aren't quite sure, you must not pump.&amp;nbsp; You
   either&amp;nbsp;avoid pumping altogether or you selectively pump messages that do not
   touch the state encapsulated by the pump.&amp;nbsp; Or you lock access to state with a
   non-recursive lock and run the risk of deadlock.
&lt;/p&gt;
&lt;p&gt;
   I have found it clarifying to think&amp;nbsp;about blocking&amp;nbsp;in&amp;nbsp;event loop concurrency
   and state machine terms, advancing from one state to the next in between waits.&amp;nbsp;
   It’s a slippery model, but particularly when working in message passing systems that
   employ event loops, it can help to identify all the familiar problems with shared
   memory, blocking, and consistency.
&lt;/p&gt;
&lt;p&gt;
   Indeed it is interesting how blocking and non-blocking systems can rapidly approach
   each other.&amp;nbsp; Starting from either extreme tempts you to tiptoe closer and closer
   to the middle.&amp;nbsp; The familiarity of the other extreme tempts you.&amp;nbsp; Until,
   alas, you just might meet in the middle.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=53e892af-d387-4283-9b10-a463f8ac3eba" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,53e892af-d387-4283-9b10-a463f8ac3eba.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=4cfad7df-ed84-46a4-a961-54aafdaeb9d7</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,4cfad7df-ed84-46a4-a961-54aafdaeb9d7.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,4cfad7df-ed84-46a4-a961-54aafdaeb9d7.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=4cfad7df-ed84-46a4-a961-54aafdaeb9d7</wfw:commentRss>
      <slash:comments>12</slash:comments>
      <title>A (brief) retrospective on transactional memory</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,4cfad7df-ed84-46a4-a961-54aafdaeb9d7.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2010/01/03/ABriefRetrospectiveOnTransactionalMemory.aspx</link>
      <pubDate>Sun, 03 Jan 2010 19:05:12 GMT</pubDate>
      <description>&lt;p&gt;
   Rewind the clock to mid-2004.&amp;nbsp; Around this time awareness about the looming “concurrency
   sea change” was rapidly growing industry-wide, and indeed within Microsoft an increasing
   number of people – myself included – began to focus on how the .NET Framework, CLR,
   and Visual C++ environments could better accommodate first class concurrent programming.&amp;nbsp;
   Of course, our colleagues in MSR and researchers in the industry more generally had
   many years’ head start on us, in some cases dating back 3+decades.&amp;nbsp; It is safe
   to say that there was no shortage of prior art to understand and learn from.
&lt;/p&gt;
&lt;p&gt;
   One piece of prior art was particularly influential on our thoughts: software transactional
   memory.&amp;nbsp; (STM, or, in short just TM.)&amp;nbsp; In fact, right around that time,
   Tim Harris’s TM work grew in notoriety (my first exposure arriving by way of OOPSLA’03’s
   proceedings, which contained &lt;a href="http://research.microsoft.com/en-us/um/people/tharris/papers/2003-oopsla.pdf"&gt;the
   “Language Support for Lightweight Transactions” paper&lt;/a&gt;).&amp;nbsp; TM was immediately
   fascinating, and simultaneously promising.&amp;nbsp; For a number of reasons:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      TM hid sophisticated synchronization mechanisms under a simple veil. 
   &lt;li&gt;
      It could be implemented using sophisticated (and scalable) techniques, again under
      a simple veil. 
   &lt;li&gt;
      It built on decades of experience in building scalable and parallel transactional
      databases. 
   &lt;li&gt;
      Among others.&amp;nbsp; But most of all, it was a bright shiny light in a sea of complexity. 
   &lt;li&gt;
      And how fortunate: Tim was a colleague in our neighboring MSR Cambridge offices (and
      still is).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   In a nutshell, TM offered declarative concurrency-safety.&amp;nbsp; You declare what you’d
   like in as few simple words as possible, and you get what you want.&amp;nbsp; In this
   case, those simple words are ‘atomic { S; }’.
&lt;/p&gt;
&lt;p&gt;
   Many people latched onto TM rapidly and simultaneously, both inside and outside of
   Microsoft.&amp;nbsp; I hacked together a little prototype built atop SSCLI (“Rotor”),
   and another architect on our team built an even more feature-rich prototype using
   MSIL rewriting.&amp;nbsp; We compared notes, began jointly exploring the design space,
   and talking more regularly with other colleagues like Tim in MSR.&amp;nbsp; Soon thereafter
   we kicked off a small working group with about a dozen architects and researchers
   from around the company, aiming to articulate what a real productized TM might look
   like.&amp;nbsp; Fun times.
&lt;/p&gt;
&lt;p&gt;
   We were eventually given the OK for an official “incubation” project, and multiple
   years’ of exploration and hard work ensued.&amp;nbsp; In fact, the fruits of a team of
   many’s labor recently got released in the form of &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee334183.aspx"&gt;a
   Community Technology Preview&lt;/a&gt; -- a good conduit for experimentation, but with no
   commitment to add it to any of Microsoft’s products.&amp;nbsp; To be clear, I had only
   a small part to play in this ambitious project, and mostly towards the start.&amp;nbsp;
   Partway through, I stepped away to do &lt;a href="http://msdn.microsoft.com/concurrency/"&gt;PLINQ
   and Parallel Extensions to .NET&lt;/a&gt;, both of which are now part of the .NET Framework
   4.0.&amp;nbsp; Dozens of amazing people played a significant role in the project over
   the years.&amp;nbsp; But I am getting way ahead of myself…
&lt;/p&gt;
&lt;p&gt;
   I’ve been away from the nitty-gritty day-to-day details of TM for about 3 years now,
   which feels sufficiently long to develop a healthy perspective on the project.&amp;nbsp;
   So here it is.&amp;nbsp; What follows is of course in no way Microsoft’s “official position”
   on the technology, but rather my own personal one.&amp;nbsp; I’ve interspersed generalizations
   with specific details because that’s just how my brain thinks about TM.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Towards the North Star&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   A wondrous property of concurrent programming is the sheer number and diversity of
   programming models developed over the years.&amp;nbsp; Actors, message-passing, data parallel,
   auto-vectorization, ...; the titles roll off the tongue, and yet none dominates and
   pervades.&amp;nbsp; In fact, concurrent programming is a multi-dimensional space with
   a vast number of worthy points along its many axes.
&lt;/p&gt;
&lt;p&gt;
   This rich history is simultaneously a blessing and a daunting curse.&amp;nbsp; But in
   any case can make for some very interesting multi-year-long immersion.&amp;nbsp; &lt;a href="http://norfolk.cs.washington.edu/htbin-post/unrestricted/colloq/details.cgi?id=768"&gt;My
   UW talk&lt;/a&gt; from 1 1/2 years ago just barely touches on the sheer breadth.
&lt;/p&gt;
&lt;p&gt;
   TM’s greatest virtue is the first word in its name: transactional.&amp;nbsp; It turns
   out that, no matter your concurrent programming model du jour, three fundamental concepts
   crop up again and again: isolation (of state), atomicity (of state transitions), and
   consistency (of those atomic transitions).&amp;nbsp; We use locks in shared-memory programming,
   coarse grained messages in message-passing, and functional programming to achieve
   all of these things in different ways.&amp;nbsp; Transactions are another such mechanism,
   sure, but more than that, transactions are an all-encompassing way of thinking about
   how programs behave at their most fundamental core.&amp;nbsp; Transaction is a religion.
&lt;/p&gt;
&lt;p&gt;
   Not everybody believes this, and of course why would they: it is an immensely subjective
   and qualitative statement.&amp;nbsp; Some will claim that models like message passing
   entirely avoid the likes of “race conditions,” and such, but this is clearly false:
   state transitions are made, complicated state invariants are erected amongst a sea
   of smaller isolated states, and care must be taken, just as in shared memory.&amp;nbsp;
   Even Argus, a beautiful early incarnation of message-passing (via promises) demands
   that messages are atomic in nature.&amp;nbsp; This property is not checked and, if done
   improperly, leads to “races in the large.”&amp;nbsp; Even Argus introduced the notion
   of transactions and persistence in the form of guardians.
&lt;/p&gt;
&lt;p&gt;
   Of course, message passing helps push you in the right direction.&amp;nbsp; It is not,
   however, a panacea.
&lt;/p&gt;
&lt;p&gt;
   I was reading my ICFP proceedings recently and was reminded of research done in the
   context of Erlang that &lt;a href="http://portal.acm.org/citation.cfm?id=1596550.1596574"&gt;supports
   this assertion&lt;/a&gt;.&amp;nbsp; In it, they apply &lt;a href="http://research.microsoft.com/en-us/projects/chess/"&gt;CHESS&lt;/a&gt;-like
   techniques (with clever search space culling) to find race conditions.&amp;nbsp; Indeed
   we use similar techniques very successfully for our message-passing programming models
   on my team here at Microsoft.
&lt;/p&gt;
&lt;p&gt;
   Transactions are terrific because they are “automatic”.&amp;nbsp; You declare the boundaries,
   and the transactional machinery takes care of the rest.&amp;nbsp; This is true of databases
   and also TM.&amp;nbsp; Countless developers in the wild write massively concurrent programs
   by issuing operations against databases: they can do this so easily because they grok
   the simple façade that transactions provide.&amp;nbsp; Numerous server-side state-based
   applications use transactions to shield programmers from the pitfalls of concurrency.&amp;nbsp;
   Behold MSDTC.&amp;nbsp; The bet we were making is that similar models would scale down
   just as well “in the small”.
&lt;/p&gt;
&lt;p&gt;
   The canonical syntactic footprint of TM is also beautiful and simple.&amp;nbsp; You say:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;…
   concurrency-safe code goes here …&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   And everything in that block is magically concurrency-safe.&amp;nbsp; (Well, you still
   need to ensure the consistency part, but isolation and atomicity are built-in.&amp;nbsp;
   Mix this with Eiffel- or&amp;nbsp;&lt;a href="http://research.microsoft.com/en-us/projects/specsharp/"&gt;Spec#&lt;/a&gt;-style
   contracts and assertions like those in .NET 4.0, run at the end of each transaction,
   and you’re well on your way to verified consistency also.&amp;nbsp; The ‘check E’ work &lt;a href="http://research.microsoft.com/en-us/um/people/simonpj/Papers/stm/stm-invariants.pdf"&gt;in
   Haskell&lt;/a&gt; was right along these lines.)&amp;nbsp; You can read and write memory locations,
   call other methods, all without worrying about whether concurrency-safety will be
   at risk.
&lt;/p&gt;
&lt;p&gt;
   For example, consider three transactions running concurrently:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;int
   x = 0, y = 0, z = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic {&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;z++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-tab-count: 4"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   No matter the order in which these run, the end result will be x == 3, y == 2, z ==
   1.
&lt;/p&gt;
&lt;p&gt;
   Contrast this elegant simplicity with the many pitfalls of locks:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      &lt;em&gt;Data races&lt;/em&gt;.&amp;nbsp; Like forgetting to hold a lock when accessing a certain
      piece of data.&amp;nbsp; And other flavors of data races, such as holding the wrong lock
      when accessing a certain piece of data.&amp;nbsp; Not only do these issues not exist,
      but the solution is not to add countless annotations associating locks with the data
      they protect; instead, you declare the scope of atomicity, and the rest is automatic. 
   &lt;li&gt;
      &lt;em&gt;Reentrancy&lt;/em&gt;.&amp;nbsp; Locks don’t compose.&amp;nbsp; Reentrancy and true recursive
      acquires are blurred together.&amp;nbsp; If a locked region expects reentrancy, usually
      due to planned recursion, life is good; if it doesn’t, life is bad.&amp;nbsp; This often
      manifests as virtual calls that reenter the calling subsystem while invariants remain
      broken due to a partial state transition.&amp;nbsp; At that point, you’re hosed. 
   &lt;li&gt;
      &lt;em&gt;Performance&lt;/em&gt;.&amp;nbsp; The tension between fine-grained locking (better scalability)
      versus coarse-grained locking (simplicity and superior performance due to fewer lock
      acquire/release calls) is ever-present.&amp;nbsp; This tension tugs on the cords of correctness,
      because if a lock is not held for long enough, other threads may be able to access
      data while invariants are still broken.&amp;nbsp; Scalability pulls you to engage in a
      delicate tip-toe right up to the edge of the cliff. 
   &lt;li&gt;
      &lt;em&gt;Deadlocks&lt;/em&gt;.&amp;nbsp; This one needs no explanation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   In a nutshell, locks are not declarative.&amp;nbsp; Not even close.&amp;nbsp; They are not
   associated with the data protected by those locks, but rather the code that accesses
   said data.&amp;nbsp; (For example: in the above code snippet, do we need three locks?&amp;nbsp;
   Or one?&amp;nbsp; Or …?&amp;nbsp; Imagine we choose three: one for each variable, x, y, and
   z.&amp;nbsp; What if we increment z, release its associated lock, and some other thread
   can now see the newly incremented z before the y and x get incremented.&amp;nbsp; Whether
   this is acceptable depends on the program.)&amp;nbsp; Sure, you can achieve atomicity
   and isolation, but only by intimately reasoning about your code by understanding the
   way they are implemented.&amp;nbsp; And if you care about performance, you are also going
   to need to think about hardware esoterica such as CMPXCHG, spin waiting, cache contention,
   optimistic techniques with version counters and memory models, ABA, and so on.
&lt;/p&gt;
&lt;p&gt;
   The contrast is stark.&amp;nbsp; Atomic-block-style transactions provide automatic serializability
   of whole regions of code, no matter what that code does, and the TM infrastructure
   does the rest, choosing between: optimistic, pessimistic, coarse, fine, etc.&amp;nbsp;
   The linearization point of a transaction is clear: the end of the atomic block.&amp;nbsp;
   TM can even adjust strategies based on the surrounding environment: hardware, dynamic
   program behavior, etc.&amp;nbsp; (“Policy”.)&amp;nbsp; In comparison to locks, TM is an order
   of magnitude simpler.&amp;nbsp; There have even been studies&amp;nbsp;whose conclusions&amp;nbsp;&lt;a href="http://www.cs.uoregon.edu/events/icse2009/images/postConf/pankratius-TMStudy-Pankratius-ICSE2009.pdf"&gt;support
   this assertion&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   (Transactions unfortunately do not address one other issue, which turns out to be
   the most fundamental of all: sharing.&amp;nbsp; Indeed, TM is insufficient – indeed, even
   dangerous – on its own is because it makes it very easy to share data and access it
   from multiple threads; first class isolation is far more important to achieve than
   faux isolation.&amp;nbsp; This is perhaps one major difference between client and server
   transactions.&amp;nbsp; Most server sessions are naturally isolated, and so transactions
   only interfere around the edges.&amp;nbsp; I’m showing my cards too early, and will return
   to this point much, much later in this essay.)
&lt;/p&gt;
&lt;p&gt;
   TM also has the attractive quality of automatic rollback of partial state updates.&amp;nbsp;
   (How did I get this far without discussing rollback?)&amp;nbsp; Concurrency aside, this
   avoids needing to write backout code to run in the face unhandled exceptions.&amp;nbsp;
   In retrospect this capability alone is almost enough to justify TM in limited quantities.&amp;nbsp;
   Reams of code “out there” contain brittle, untested, and, therefore, incorrect error
   handling code.&amp;nbsp; We have seen such code lead to problems ranging in severity:
   reliability issues leading to data loss, security exploits, etc.&amp;nbsp; Were we to
   replace all those try/catch/rethrow blocks of code with transactions, we could do
   away with this error prone spaghetti.&amp;nbsp; We’d also eliminate try/filter exploits
   thanks to Windows/Win32 2-pass SEH.&amp;nbsp; Sometimes I wish we focused on this simple
   step forward, forgot about concurrency-safety, and baby stepped our way forward.&amp;nbsp;
   Likely it wouldn’t have been enough, but I still wonder to this day.
&lt;/p&gt;
&lt;p&gt;
   We also toyed with the ability to replace reliability-oriented CER blocks with transactions.&amp;nbsp;
   As you go through a transaction, there is a log of forward progress and how to undo
   it.&amp;nbsp; So no matter the kind of failure, including OOM, you can rollback the partial
   state updates with zero allocation required.
&lt;/p&gt;
&lt;p&gt;
   At some point we began describing an ‘atomic’ block as though the program used a single
   global lock for all its concurrency operations.&amp;nbsp; This would be grossly inefficient,
   of course, and fails to capture the precise isolation and rollback properties, but
   nevertheless conveys the basic idea.&amp;nbsp; It also, as an aside, foreshadows a few
   of the difficult problems that lie ahead, namely strong vs. weak atomicity.&amp;nbsp;
   Even though there is only one, if you forget to hold this one global lock while accessing
   shared data, you’ve still got a data race on your hands.&amp;nbsp; This model won’t save
   you.&amp;nbsp; We will return to this later on.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Tough Decisions: Life as a Starving Artist &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   We faced some programming model decisions requiring artistic license early on.
&lt;/p&gt;
&lt;p&gt;
   One that we quickly decided was whether to automatically roll back a transaction in
   response to an unhandled exception thrown from within.&amp;nbsp; Such as with this code:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (p)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
   new Exception(“Whoops”);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   If p evaluates to true, and hence an unhandled exception thrown, should that x++ be
   rolled back?
&lt;/p&gt;
&lt;p&gt;
   Most on the team said “Yes” as a gut reaction, whereas some argued we should require
   the programmer to catch-and-rollback by hand.&amp;nbsp; We settled on the automatic approach
   because it seemed to do what you would expect in all the cases we looked at.&amp;nbsp;
   Your transaction failed to complete normally and consistently.&amp;nbsp; We also debated
   whether to support a unilateral “Transaction.Abort()” capability; while we agreed
   a “Transaction.Commit()” would be silly – the only way to commit a transaction being
   to reach its end non-exceptionally – the jury remained split on unilateral abort.&amp;nbsp;
   We eventually found that, particularly when nesting is involved, the ability to detect
   a dire problem with the universe and bail unilaterally can be useful.
&lt;/p&gt;
&lt;p&gt;
   And we also hit some tough snags early on.&amp;nbsp; Some were trivial, like what happens
   when an exception is thrown out of an atomic block.&amp;nbsp; Of course that exception
   was likely constructed within the atomic block (‘throw new SomeException()’ being
   the most common form of ‘throw’), so we decided we probably need to smuggle at least
   some of that exception’s state out of the transaction.&amp;nbsp; Like its stack trace.&amp;nbsp;
   And perhaps its message string.&amp;nbsp; I wrote the initial incarnation of the CLR exception
   subsystem support, and stopped at shallow serialization across the boundary.&amp;nbsp;
   But this was a slippery slope, and eventually the general problem was seen, leading
   to more generalized nesting models (which I shall describe briefly below).&amp;nbsp; Another
   snag, which was quite non-trivial, was the impact to the debugging experience.&amp;nbsp;
   Depending on various implementation choices – like in-place versus buffered writes
   – you may need to teach the debugger about TM intrinsically.&amp;nbsp; And some of the
   challenges were fundamental to building a first class TM implementation.&amp;nbsp; Clearly
   the GC needed to know about TM and its logging, because it needs to keep both the
   “before” and “after” state of the transaction alive, in case it needed to roll back.&amp;nbsp;
   The JIT compiler was very quickly splayed open and on the surgery table.&amp;nbsp; And
   so on.
&lt;/p&gt;
&lt;p&gt;
   Throughout, it became abundantly clear that TM, much like generics, was a systemic
   and platform-wide technology shift.&amp;nbsp; It didn’t require type theory, but the road
   ahead sure wasn’t going to be easy.
&lt;/p&gt;
&lt;p&gt;
   So we knocked down many early snags, and kept plowing forward, eagerly and excitedly.&amp;nbsp;
   None of these challenges were insurmountable.&amp;nbsp; We remained hopeful and happy
   (perhaps even blissful) to continue exploring the space of possible solutions.&amp;nbsp;
   More irksome snags lurked right around the corner, however.&amp;nbsp; And little did we
   know that some decisions we were about to make would subject us to some of the biggest
   such snags.&amp;nbsp; TM’s greatest feature – slap an atomic around a block of code and
   it just gets better – would turn out to be its greatest challenge… but alas, I am
   again jumping ahead; more on that later.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Turtles, but How Far Down?&amp;nbsp; Or, Bounded vs. Unbounded Transactions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Not all transactions are equal.&amp;nbsp; There is a broad spectrum of TMs, ranging from
   those that are bounded to updating, say, 4 memory locations or cache lines, to those
   that are entirely unbounded.&amp;nbsp; Indeed TM blurs together with other hardware-accelerated
   synchronization techniques, like speculative lock elision (SLE).&amp;nbsp; The more constrained
   TM models are often hardware-hybrids, and the limitations imposed are typically due
   to physical hardware constraints.&amp;nbsp; Models can be pulled along other axes, however,
   such as whether memory locations must be tagged in order to be used in a transaction
   or not, etc.&amp;nbsp; Haskell requires this tagging (via TVars) so that side-effects
   are evident in the type system as with any other kind of monad.
&lt;/p&gt;
&lt;p&gt;
   We quickly settled on unbounded transactions.&amp;nbsp; Everything else looked like multi-word
   CAS and, although we knew multi-word CAS would be immensely useful for developing
   new lock-free algorithms, our aim was to build something radically new and with broader
   appeal.&amp;nbsp; If we ended up with a hardware-hybrid, we would expect the software
   to pick up the slack; you’d get nice acceleration within the hardware constraints,
   and then “fall off the silent cliff” to software emulation thereafter.&amp;nbsp; Thus
   the unbounded approach was chosen.
&lt;/p&gt;
&lt;p&gt;
   In hindsight, this was a critical decision that had far-reaching implications.&amp;nbsp;
   And to be honest, I now frequently doubt that it was the right call.&amp;nbsp; We had
   our hearts in the right places, and the entire industry was trekking down the same
   path at the same time (with the notable exception of Haskell).&amp;nbsp; But with the
   wisdom of age and hindsight, I do believe limited forms of TM could be wildly successful
   at particular tasks and yet would have avoided many of the biggest challenges with
   unbounded TM.
&lt;/p&gt;
&lt;p&gt;
   And believe me: many such challenges arose in the ensuing months.
&lt;/p&gt;
&lt;p&gt;
   An example of one challenge that didn’t threaten the model of TM per se, but sure
   did make our lives more difficult, is the compilation strategy we were forced to adopt.&amp;nbsp;
   Transactions cost something.&amp;nbsp; To transact a read or write entails a non-trivial
   amount of extra work; we spent a lot of time optimizing away redundant work, and developing
   new optimizations that reduced the overhead of TM.&amp;nbsp; But at the end of the day,
   the cost is not zero – and in fact, the common case is far from it.&amp;nbsp; Imagine
   you have an unbounded transaction model and are faced with compiling a particular
   method from MSIL to native code.&amp;nbsp; A simple separate-module -based compiler (i.e.
   not whole-program) will not necessarily know whether this method will get called from
   a transaction, or from non-transactional code, such that in the worst case the method
   must be prepared for transactional access.&amp;nbsp; There are a variety of techniques
   to use to produce code that supports both: the two extremes are (1) cloning, or (2)
   sharing w/ conditional dynamic checking.&amp;nbsp; Neither extreme is particularly attractive,
   and this choice represents a classic space-time tradeoff that entails finding a reasonable
   middle ground.&amp;nbsp; A JIT compiler can dynamically produce the version that is needed
   at the moment, but offline compilers – like the CLR’s NGEN – do not have this luxury.&amp;nbsp;
   And within Microsoft at least, and among shrink-wrap ISVs, offline compilation is
   of greater importance than JIT compilation.&amp;nbsp; For better or for worse.
&lt;/p&gt;
&lt;p&gt;
   The model of unbounded transactions is the hard part.&amp;nbsp; You surround any arbitrary
   code with ‘atomic { … }’ and everything just works.&amp;nbsp; It sounds beautiful.&amp;nbsp;
   But just think about what can happen within a transaction: memory operations, calls
   to other methods, P/Invokes to Win32, COM Interop, allocation of finalizable objects,
   I/O calls (disk, network, databases, console, …), GUI operations, lock acquisitions,
   CAS operations, …, the list goes on and on.&amp;nbsp; Versus bounded transactions, where
   we could say something like: if you do more than N things, the transaction will fail
   to run – deterministically.
&lt;/p&gt;
&lt;p&gt;
   Unbounded really was the golden nugget.&amp;nbsp;&amp;nbsp; But we should not be shy about
   what this decision implies.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Implementing the Idea&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   This leads me to a brief tangent on implementation.&amp;nbsp; Given that we didn’t implement
   TM with a single global lock, as the naïve mental model above suggests, you may wonder
   how we actually did do it.&amp;nbsp; Three main approaches were seriously considered:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      IL rewriting.&amp;nbsp; Use a tool that passes over the IL post-compilation to inject
      transaction calls. 
   &lt;li&gt;
      Hook the (JIT) compiler.&amp;nbsp; The runtime itself would intrinsically know how to
      inject such calls. 
   &lt;li&gt;
      Library-based.&amp;nbsp; All transactional operations would be explicit calls into the
      TM infrastructure.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   Approaches #1 and #2 would look similar, but the latter would be quite different.&amp;nbsp;
   Instead of: 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Or:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Atomic.Run(()
   =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;});&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   You might say something like:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Atomic.Run(()
   =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Atomic.Write(Atomic.Read(ref
   x) + 1); 
   &lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;});&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   With enough language work, we could have tried to desugar the latter into the former,
   but when you start crossing method boundaries, everything gets more complicated.&amp;nbsp;
   (Do you create transactional clones of every method, and rewrite calls from ordinary
   methods to the transactional clone?&amp;nbsp; This is easy to do with a rewriter or compiler,
   but quite difficult with a pure library approach.)&amp;nbsp;&amp;nbsp; We also knew we’d need
   to do some very sophisticated compiler optimizations to get TM’s performance to the
   point of acceptable.&amp;nbsp; So we chose approach #2 for our “real” prototype, and never
   looked back.
&lt;/p&gt;
&lt;p&gt;
   After this architectural approach was decided, a vast array of interesting implementation
   choices remained.
&lt;/p&gt;
&lt;p&gt;
   We moved on to building the primitive library with all the TM APIs that the JIT would
   introduce calls into.&amp;nbsp; We quickly settled an approach much like Harris’s (and,
   at the time, pretty much the industry/research standard): optimistic reads, in-place
   pessimistic writes, and automatic retry.&amp;nbsp; That means reads do not acquire locks
   of any sort, and instead, once the end of the transaction has been reached, all reads
   are validated; if any locations read have been modified concurrently (or an uncommitted
   value was read), the whole transaction is thrown away and reattempted from the start.&amp;nbsp;
   Writes work like locks.&amp;nbsp; This approach makes reads cheap: a single read consists
   of reading the value, and a version number whose address is at a statically known
   offset.&amp;nbsp; No interlockeds.&amp;nbsp; This is great since reads typically far outnumber
   writes.&amp;nbsp; Down the line, we explored adding more sophisticated policy than this,
   which I will detail in brief below.
&lt;/p&gt;
&lt;p&gt;
   So the compiler would inject hooks for the above code like so:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;while
   (true) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;TX
   tx = new TX();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tx.OpenReadOptimistic(ref
   x);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   tmp = x;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tx.OpenWritePessimistic(ref
   x);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x
   = (tmp + 1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (!tx.Validate())&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continue;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tx.Commit();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tx.Rollback();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Notice there are some obvious overheads in here:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      The atomic block becomes a loop (to support automated retry). 
   &lt;li&gt;
      A new transaction must be allocated and likely placed in TLS (if methods are called). 
   &lt;li&gt;
      A try/catch block is used to initiate rollback on unhandled exceptions. 
   &lt;li&gt;
      Each unique location read in a block requires at least one call to OpenReadOptimistic. 
   &lt;li&gt;
      Each unique location written requires at least one call to OpenWritePessimistic. 
   &lt;li&gt;
      Each location read must be validated (at Validate), and finally the transaction is
      committed (at Commit). 
   &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   Much of the work in the compiler was meant to reduce these overheads.&amp;nbsp; For example,
   if the same location is read multiple times, there’s no need to call OpenReadOptimistic
   more than once.&amp;nbsp; If the compiler can statically detect this, it may elide some
   of the calls.&amp;nbsp; If the same location is read and then written – as in the above
   example – only the write lock must be acquired.&amp;nbsp; If no methods are called, the
   transaction object can be enregistered, and we needn’t add it to TLS so long as the
   exception trap code knows how to move it from register to TLS on demand.&amp;nbsp; Et
   cetera.
&lt;/p&gt;
&lt;p&gt;
   There are other overheads that are not so obvious.&amp;nbsp; Optimistic reads mandate
   that there is a version number for each location somewhere, and pessimistic writes
   mandate that there is a lock for each location somewhere.
&lt;/p&gt;
&lt;p&gt;
   A straightforward technique is to use a hashing scheme to associate locations with
   this auxiliary data: each address is hashed to index into a table of version numbers
   and locks.&amp;nbsp; This leads to false sharing, of course, but reduces space overhead
   and makes lookup fast.&amp;nbsp; Unfortunately, in a garbage collected environment, addresses
   are not stable and therefore hashing becomes complicated.&amp;nbsp; You can use object
   hash codes for this purpose, but .NET hash codes are overridable; and generating them
   is not nearly as cheap as using the memory location’s address, which by definition
   is already in-hand.&amp;nbsp; Other alternatives of course exist.&amp;nbsp; You can associate
   version numbers and locks with the objects themselves, just like monitors and object
   headers/sync-blocks in the CLR: this provides object-granularity locking.&amp;nbsp; Ahh,
   the age old tension of fine vs. coarse grained locking comes up again.
&lt;/p&gt;
&lt;p&gt;
   We eventually realized we’d want both optimistic and pessimistic reads, the latter
   of which worked a lot like reader/writer locks.&amp;nbsp; We crammed all these into a
   clever little word-sized data structure which worked a lot like Vista’s SRWL data
   structure.&amp;nbsp; Except that it also contained a version number.
&lt;/p&gt;
&lt;p&gt;
   It was always surprising to me what strange things in the runtime we bumped up against.&amp;nbsp;
   We realized a nice GC optimization: instead of keeping strong references to all intermediary
   states in a transaction log, we could keep weak references to all but the “before”
   and “after” state.&amp;nbsp; This is important when transacting synthetic situations like
   this:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;static
   BigHonkinFoo s_f;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;…&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; 1000000; i++)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s_f
   = new BigHonkinFoo();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Of course you wouldn’t write that code exactly.&amp;nbsp; But there’s no need to keep
   alive all but the s_f that existed prior to entering the atomic block and the current
   one at any given time.&amp;nbsp; But this leads to particularly hairy finalization issues.&amp;nbsp;
   If a finalizable object is allocated within a transaction (say BigHonkinFoo), and
   is then reclaimed, its Finalize() method will be scheduled to run on a separate thread.&amp;nbsp;
   Yet the transaction log may contain references to it.&amp;nbsp; Thus there is a race between
   the transaction’s final outcome and the invocation of the finalizer.&amp;nbsp; We came
   up with a clever solution for this, but there were countless other clever solutions
   for various things not worth diving too deep into.
&lt;/p&gt;
&lt;p&gt;
   Hacking is fun.&amp;nbsp; However, it was not going to be what made or broke TM as a model.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Disillusionment Part I: the I/O Problem&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   It wasn’t long before we realized another sizeable, and more fundamental, challenge
   with unbounded transactions.&amp;nbsp; Finalizers touched on this.&amp;nbsp; What do we do
   with atomic blocks that do not simply consist of pure memory reads and writes?&amp;nbsp;
   (In other words, the majority of blocks of code written today.)&amp;nbsp; This was not
   just a pesky question of how to compile a piece of code, but rather struck right at
   the heart of the TM model.
&lt;/p&gt;
&lt;p&gt;
   You already saw the OpenReadOptimistic, OpenWritePessimistic, Validate, Commit, and
   Rollback pseudo-TM infrastructure calls, each of which operated on memory locations.&amp;nbsp;
   But what about a read or write from a single block or entire file on the filesystem?&amp;nbsp;
   Or output to the console?&amp;nbsp; Or an entry in the Event Log?&amp;nbsp; What about a web
   service call across the LAN?&amp;nbsp; Allocation of some native memory?&amp;nbsp; And so
   on.&amp;nbsp; Ordinarily these kinds of operations will be composed with other memory
   operations, with some interesting invariant relationship holding between the disparate
   states.&amp;nbsp; A transaction comprised of a mixture still ought to remain atomic and
   isolated.
&lt;/p&gt;
&lt;p&gt;
   The answer seemed clear.&amp;nbsp; At least in theory.&amp;nbsp; The transaction literature,
   including &lt;a href="http://www.amazon.com/exec/obidos/ASIN/1558601902/bluebytesoftw-20"&gt;Reuter
   and Gray’s classic&lt;/a&gt;, had a simple answer for such things: on-commit and on-rollback
   actions, to perform or compensate the logical action being performed, respectively.&amp;nbsp;
   (Around this same time, the Haskell folks were creating just this capability in their
   STM, where ‘onCommit’ and ‘onRollback’ would take arbitrary lambdas to execute the
   said operation at the proper time.)&amp;nbsp; Because we were working primarily in .NET
   – with a side project targeting C++ -- we decided to use the new System.Transactions
   technology in 2.0 to hook into inherently transactional resources, like transacted
   NTFS, registry, and, of course, databases.
&lt;/p&gt;
&lt;p&gt;
   (Digging through my blog, I found &lt;a href="http://www.bluebytesoftware.com/blog/2006/06/20/AVolatileTransactionResourceManagerForMemoryAllocationdeallocation.aspx"&gt;this
   article&lt;/a&gt; written back in June 2006 about building a volatile resource manager for
   memory allocation/free operations, just as an example.)
&lt;/p&gt;
&lt;p&gt;
   This worked, though we were quite obviously swimming upstream.&amp;nbsp; Numerous challenges
   confronted us.
&lt;/p&gt;
&lt;p&gt;
   A significant problem was that not all operations are inherently transactional, so
   in many cases we were faced with the need to add faux transactions on top of existing
   non-transactional services.&amp;nbsp; (Already-transactional services were easy, like
   databases.&amp;nbsp; Except that mixing fine-grain TM transactions with distributed DTC
   transactions makes my skin crawl.)&amp;nbsp; For example, how would you undo a write to
   the console?&amp;nbsp; Well, you can’t, really.&amp;nbsp; So we decided maybe the right default
   for Console.WriteLine was to use an on-commit action to perform the actual write only
   once the transaction had committed.
&lt;/p&gt;
&lt;p&gt;
   But in even thinking this thought, we realized we were standing on shaky ground.&amp;nbsp;
   What if the WriteLine was followed by something like a ReadLine, for example, where
   the program was meant to wait for the user to enter something into the console (likely
   in response to the prompt output by WriteLine)?&amp;nbsp; (This example is a toy, of course,
   but represents a more fundamental pattern common in networked programs.)&amp;nbsp; The
   basic problem was immediately clear.&amp;nbsp; Adding isolation to an existing non-isolated
   operation is not always behavior-preserving, particularly when I/O is involved.&amp;nbsp;
   Sometimes it is necessary to step outside of the isolation that would otherwise get
   poured on top by a simple transactional model.
&lt;/p&gt;
&lt;p&gt;
   This particular problem isn’t specific to traditional I/O per se.
&lt;/p&gt;
&lt;p&gt;
   Foreign function interface calls through.NET’s P/Invoke suffer from like problems.&amp;nbsp;
   A call to CreateEvent may be compensatable (via an on-rollback action) with a call
   to CloseHandle.&amp;nbsp; But this is flawed.&amp;nbsp; Once that event’s HANDLE is requested,
   and/or it is passed to other Win32 APIs like MsgWaitForMultipleObjects, then the isolation
   of the faux transaction is broken, and real state must be provided to the Win32 APIs.&amp;nbsp;
   And if another thread were to look up that HANDLE – perhaps through a name given to
   it in the call to CreateEvent – it may be able to see and interact with that event
   before the enclosing transaction has been committed.&amp;nbsp; The abstraction leaks.&amp;nbsp;
   And even if the abstraction is perfect, it is obvious there’s quite a bit of work
   to be had in order to transact all the touch points between .NET and Win32, of which
   there are many.&amp;nbsp; And I mean many.
&lt;/p&gt;
&lt;p&gt;
   Other issues wait just around the corner.&amp;nbsp; For example, how would you treat a
   lock block that was called from within a transaction?&amp;nbsp; (You might say “that’s
   just not supported”, but when adding TM to an existing, large ecosystem of software,
   you’ve got to draw the compatibility line somewhere.&amp;nbsp; If you draw it too carelessly,
   large swaths of existing software will not work; and in this case, that often meant
   that we claimed to provide unbounded transactions, and yet we would be placing bounds
   on them such that a lot of existing software could not be composed with transactions.&amp;nbsp;
   Not good.)&amp;nbsp; A seemingly straightforward answer is to treat a lock block like
   an atomic block.&amp;nbsp; So if you encounter:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lock
   (obj) { … }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   it is logically transformed into:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { … }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   On the face of it, this looks okay.&amp;nbsp; (Forget problems like freeform use of Monitor.Enter/Exit
   for now.)&amp;nbsp; We’re strengthening the atomicity and isolation, so what could go
   wrong?&amp;nbsp; Well, it turns out that examples like this can also suffer from the “too
   much isolation” problem.&amp;nbsp; Adding transactions to a lock-block extends the lifetime
   of the isolation of that particular block’s effects, possibly leading to lack of forward
   progress.&amp;nbsp; In fact, you don’t need locks to illustrate the problem.&amp;nbsp; Imagine
   a simple lock-free algorithm that communicates between threads using shared variables:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;volatile
   int flag = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;…&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;flag
   = 1;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while
   (flag != 1) ;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;while
   (flag == 1) ;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;flag
   = 2;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   If you invoke this code from within a transaction (on each thread), you’re apt to
   lead to deadlock.&amp;nbsp; Both transactions’ effects will be isolated from the others’,
   whereas we are quite obviously intending to publish the updates to the flag variable
   immediately.
&lt;/p&gt;
&lt;p&gt;
   Anyway, the whole lock thing is a bit of a digression.&amp;nbsp; The simple fact is that
   very little .NET code would actually run inside an atomic block but for things like
   collections and pure computations due to the I/O problem.&amp;nbsp; You can develop one-off
   solutions for each problem that arises – and indeed we did so for many of them – and
   even hang those solutions underneath one general framework – like System.Transactions
   – but you cannot help but eventually become overwhelmed by the totality of the situation.&amp;nbsp;
   The team experimented with static checking to turn these dynamic failures into static
   ones, but this only marginally improved matters.
&lt;/p&gt;
&lt;p&gt;
   I could go on and on about the I/O problem, its various incarnations, and what we
   did about it.&amp;nbsp; Instead I will sum it up: this problem was, and still is, the
   “elephant in the room” threatening unbounded TM’s broader adoption.
&lt;/p&gt;
&lt;p&gt;
   The question ultimately boils down to this: is the world going to be transactional,
   or is it not?
&lt;/p&gt;
&lt;p&gt;
   Whether unbounded transactions foist unto the world will succeed, I think, depends
   intrinsically on the answer to this question.&amp;nbsp; It sure looked like the answer
   was going to be “Yes” back when transactional NTFS and registry was added to Vista.&amp;nbsp;
   But the momentum appears to have slowed dramatically.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Nesting&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Let’s get back to some fun, less depressing material.&amp;nbsp; There are more surprises
   lurking ahead.
&lt;/p&gt;
&lt;p&gt;
   I already mentioned a great virtue of transactions is their ability to nest.&amp;nbsp;
   But I neglected to say how this works.&amp;nbsp; And in fact when we began, we only recognized
   one form of nesting.&amp;nbsp; You’re in one atomic block and then enter into another
   one.&amp;nbsp; What happens if that inner transaction commits or rolls back, before the
   fate of the outer transaction is known?&amp;nbsp; Intuition guided us to the following
   answer:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      If the inner transaction rolls back, the outer transaction does not necessarily do
      so.&amp;nbsp; However, no matter what the outer transaction does, the effects of the inner
      will not be seen. 
   &lt;li&gt;
      If the inner transaction commits, the effects remain isolated in the outer transaction.&amp;nbsp;
      It “commits into” the outer transaction, we took to saying.&amp;nbsp; Only if the outer
      transaction subsequently commits will the inner’s effects be visible; if it rolls
      back, they are not.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   For example, consider this code:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;void
   f() {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void
   g() {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { // Tx0&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { // Tx1&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try
   {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (p1)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;g();&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
   new BarException();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}
   catch {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (p0)&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;throw;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (p2)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
   new FooException();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Imagine x = y = 0 at the start, and we invoke f.&amp;nbsp; Many outcomes are possible.
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      If p1 is true, g will throw an exception, aborting Tx1’s write to y. There are then
      two possibilities.&amp;nbsp; (1)If p0 is true, the exception is repropagated and Tx0 will
      also abort, rolling back its write to x; this leaves x == y == 0.&amp;nbsp; (2) If p0
      is false, the exception is swallowed, and Tx0 proceeds to committing its write to
      x; this leaves x == 1, whereas y == 1. 
   &lt;li&gt;
      If p1 is false, on the other hand, g will not throw anything.&amp;nbsp; Tx1 will commit
      its write to y “into” the outer transaction Tx0.&amp;nbsp; One of two outcomes will now
      occur depending on the value of p2.&amp;nbsp; (1) If p2 is true, an exception is thrown
      out of f, and Tx0 rolls back both the inner transaction Tx1’s effects and its own,
      leaving x == y == 0.&amp;nbsp; (2) Else, f completes ordinarily, and Tx0 commits both
      Tx1’s and its own effects, leading to x == y == 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   We expected most peoples’ intuition to match this behavior.
&lt;/p&gt;
&lt;p&gt;
   The canonical working example was a BankAccount class:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;class
   BankAccount&lt;br&gt;
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;decimal
   m_balance;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   void Deposit(decimal delta) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { m_balance += delta; }&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static void Transfer(&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BankAccount
   a, BankAccount b, decimal delta) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;a.Deposit(-delta);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;b.Deposit(delta);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   This was an illustrative and beautiful example.&amp;nbsp; It made beautiful slide-ware.&amp;nbsp;
   We are composing the Deposit operations of two separate bank accounts into a single
   Transfer method.&amp;nbsp; Of course doing the a.Deposit(-delta) and b.Deposit(delta)
   must be made atomic, else a failure could either lead to missing money, and/or someone
   could witness the world with the money in transit (and nowhere except for one a thread’s
   stack) rather than having been transferred atomically.&amp;nbsp; And building the same
   thing with locks is frustratingly difficult: using fine-grained per-account locks
   can lead to deadlock very quickly.
&lt;/p&gt;
&lt;p&gt;
   Intuitively we walked down many variants of this mode of nesting.&amp;nbsp; We reacquainted
   ourselves with Moss’s &lt;a href="http://portal.acm.org/citation.cfm?id=3529"&gt;great dissertation
   on the topic&lt;/a&gt;, and remembered this intuitive nesting mode as closed nested transactions.&amp;nbsp;
   And we shortly recognized the need for another mode: &lt;a href="http://www.cs.utah.edu/wmpi/2006/final-version/wmpi-posters-1-Moss.pdf"&gt;open
   nested transactions&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   To motivate the need for open nesting, imagine we’ve got a hashtable whose physical
   storage is independent from its logical storage.&amp;nbsp; Resizing the table of buckets,
   for example, has little to do with whether a particular {key, value} pair exists within
   those buckets.&amp;nbsp; The resizing operation, in fact, is logically idempotent and
   isolated: the same set of keys will exist within the table both before and after such
   an operation.&amp;nbsp; So we can actually commit the physical effects of such an operation
   eagerly.&amp;nbsp; With a naïve TM implementation, two independent keys hashing to the
   same bucket will conflict, and the reads and writes for such operations will live
   as long as the enclosing user-level transactions.&amp;nbsp; Instead, we can serialize
   logical operations with respect to one another at a “higher level” than physically
   independent operations do, leading to greater concurrency.&amp;nbsp; Two transactions
   will only conflict in long-running transactions if they truly operate on the same
   keys, rather than just happening to hash to the same bucket.
&lt;/p&gt;
&lt;p&gt;
   Open nesting forced us to contemplate the sharing of state between outer and inner
   transactions more deliberately, and gave us some troubles syntactically.&amp;nbsp; We
   had wanted to say:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   { // ordinary closed nesting.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Foo
   f = new Foo();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic(open)
   { /// open nesting.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;…
   f? …&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   But is it really legal for the inner transaction here to access the ‘f’, which has
   been constructed and is presumably uncommitted in the outer transaction?&amp;nbsp; With
   closed nested transactions there is lock compatibility between the outer and inner
   transactions.&amp;nbsp; An inner closed nested transaction can of course read a memory
   location write-locked by the outer transaction, for example.&amp;nbsp; However, the same
   must not true of open nesting, because an open nested transaction commits “to the
   world” rather than into its outer transaction.&amp;nbsp; Allowing it to read and then
   potentially publish uncommitted state would violate serializability.&amp;nbsp; It’s possible
   that the inner open nested transaction will commit, whereas the outer will roll back.&amp;nbsp;
   (The reverse situation is equally problematic.)&amp;nbsp; And yet it’s darn useful to
   pass state from an outer to an inner transaction – and indeed, often impossible to
   do anything otherwise – yet what if the key itself were a complicated object graph
   rather than value, and the key bleeds across transaction boundaries?
&lt;/p&gt;
&lt;p&gt;
   Many issues like this arose.&amp;nbsp; Our straightforward answer was that only pass-by-value
   worked across such a boundary.&amp;nbsp; I don’t think we ever found nirvana here.
&lt;/p&gt;
&lt;p&gt;
   We developed other transaction modes also.
&lt;/p&gt;
&lt;p&gt;
   As we added data parallel operations within a nested transaction, we realized that
   we’d need something a lot like closed nesting but with special accommodation for intra-transaction
   parallelism.&amp;nbsp; This led us to &lt;a href="http://www.cs.rochester.edu/meetings/TRANSACT07/papers/agrawal.pdf"&gt;parallel
   nested transactions&lt;/a&gt;, enabling lock sharing from a parent to its many data parallel
   children.&amp;nbsp; These children could not communicate with one another other than to
   “commit into” the parent, and subsequently reforking, thereby ensuring non-interference
   between them.&amp;nbsp; Of course children could share read-locks amongst one another,
   just not write locks.
&lt;/p&gt;
&lt;p&gt;
   And we continued to reject the temptation of adding weakened serializability modes
   a la relational databases (unrepeatable reads, etc).&amp;nbsp; Although we expected this
   to arise out of necessity with time, it never did; the various nesting modes we provided
   seemed to satisfy the typical needs.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;A Better Condition Variable&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Here’s a brief aside on one of TM’s bonus features.
&lt;/p&gt;
&lt;p&gt;
   Some TM variants also provide for “condition variable”-like facilities for coordination
   among threads.&amp;nbsp; I think Haskell was the first such TM to provide a ‘retry’ and
   ‘orElse’ capability.&amp;nbsp; When a ‘retry’ is encountered, the current transaction
   is rolled back, and restarted once the condition being sought becomes true.&amp;nbsp;
   How does the TM subsystem know when that might be?&amp;nbsp; This is an implementation
   detail, but one obvious choice is to monitor the reads that occurred leading up to
   the ‘retry’ – those involved in the evaluation of the predicate – and once any of
   them changes, to reschedule that transaction to run.&amp;nbsp; Of course, it will reevaluate
   the predicate and, if it has become false, the transaction will ‘retry’ again.
&lt;/p&gt;
&lt;p&gt;
   A simple blocking queue could be written this way.&amp;nbsp; For example:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;object
   TakeOne()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (Count == 0)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;retry;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   Pop();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   If, upon entering the atomic block, Count is witnessed as being zero, we issue a retry.&amp;nbsp;
   The transaction subsystem notices we read Count with a particular version number,
   and then blocks the current transaction until Count’s associated version number changes.&amp;nbsp;
   The transaction is then rescheduled, and races to read Count once again.&amp;nbsp; After
   Count is seen as non-zero, the Pop is attempted.&amp;nbsp; The Pop, of course, may fail
   because of a race – i.e. we read Count optimistically without blocking out writers
   – but the usual transaction automatic-reattempt logic will kick in to mask the race
   in that case.
&lt;/p&gt;
&lt;p&gt;
   The ‘orElse’ feature is a bit less obvious, though still rather useful.&amp;nbsp; It enables
   choice among multiple transactions, each of which may end up issuing a ‘retry’.&amp;nbsp;
   I don’t think I’ve seen it in any TMs except for ours and Haskell’s.
&lt;/p&gt;
&lt;p&gt;
   To illustrate, imagine we’ve got 3 blocking queues like the one above.&amp;nbsp; Now imagine
   we’d like to take from the first of those three that becomes non-empty.&amp;nbsp; ‘orElse’
   makes this simple:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;BlockingQueue
   bq1 = …, bq2 = …, bq3 = …;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;object
   obj =&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;orElse
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bq1.TakeOne(),&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bq2.TakeOne(),&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bq3.TakeOne()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   While ‘orElse’ is perhaps an optional feature, you simply can’t write certain kinds
   of multithreaded algorithms without ‘retry’.&amp;nbsp; Anything that requires cross-thread
   communication would need to use spin variables.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Deliberate Plans of Action: Policy&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   I waved my hands a bit above perhaps without you even knowing it.&amp;nbsp; When I talk
   about optimistic, pessimistic, and automatic retry, I am baking in a whole lot of
   policy.&amp;nbsp; It turns out there is a wide array of techniques.&amp;nbsp; The simplest
   question we faced early on was, when an optimistic read fails to validate at the end
   of a transaction, when should we reattempt execution of that transaction?
&lt;/p&gt;
&lt;p&gt;
   The naïve answer is “immediately”.&amp;nbsp; But obviously that would lead to livelock
   under some conditions.&amp;nbsp; A more reasonable answer is “spin for N cycles and then
   retry”.&amp;nbsp; But this too can lead to livelock.&amp;nbsp; A better answer is to either
   choose some random strategy, or to make an intelligent adaptive choice.&amp;nbsp; We experimented
   with many such variants, including random backoff, sophisticated waiting and signaling
   based on the memory locations in question, among others.&amp;nbsp; We even played games
   like giving transactions karma points for cooperatively acquiescing to other competing
   transactions, and allowing those transactions with the most karma points to make more
   forward progress before interrupting them.
&lt;/p&gt;
&lt;p&gt;
   &lt;a href="http://lpd.epfl.ch/kapalka/files/robust-cm-scool05.pdf"&gt;A few good papers&lt;/a&gt; supplied
   useful (and entertaining) reading material on the topic, but to be honest, nobody
   had a good answer at the time.&amp;nbsp; Thankfully these are all implementation details.&amp;nbsp;
   So we were free to experiment.
&lt;/p&gt;
&lt;p&gt;
   Deadlock breaking also requires policy.&amp;nbsp; Thankfully we can actually roll back
   the effects of transactions engaged in a deadly embrace with TM, so we merely need
   to know how often to run the deadlock detection algorithm.&amp;nbsp; There was a similar
   problem when deciding to back off outer layers of nesting, and in fact this becomes
   more complicated when deadlocks are involved.&amp;nbsp; Imagine:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;span style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;span style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   This deadlock-prone example is tricky because rolling back the inner-most transactions
   won’t be sufficient to break the deadlock that may occur.&amp;nbsp; Instead the TM policy
   manager needs to detect that multiple levels of nesting are involved and must be blown
   away in order to unstick forward progress.
&lt;/p&gt;
&lt;p&gt;
   Another variant that went beyond deciding when to favor one transaction over another
   was to upgrade to pessimistic locking if optimistic let us down.&amp;nbsp; The whole justification
   behind optimistic is that, …well, we’re optimistic that conflicts won’t happen.&amp;nbsp;
   So it seems reasonable that, if they do occur, we fall back to something more, …well,
   pessimistic.&amp;nbsp; There is a dial here too.&amp;nbsp; Perhaps you only want to fall back
   to pessimistic after failing optimistically N times in a row, where N &amp;gt; 1.&amp;nbsp;
   As I mentioned above, our single-word lock associated with each object supported both
   locking and versioning cheaply.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Disillusionment Part II: Weak or Strong Atomicity?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   All along, we had this problem nipping at our heels.&amp;nbsp; What happens if code accesses
   the same memory locations from inside and outside a transaction?&amp;nbsp; We certainly
   expected this to happen over the life of a program: state surely transitions from
   public and shared among threads to private to a single thread regularly.&amp;nbsp; But
   if some location were to be accessed transactionally and non-transactionally concurrently,
   at once, we’d (presumably) have a real mess on our hands.&amp;nbsp; A supposedly atomic,
   isolated, etc. transaction would no longer be protected from the evils of racey code.
&lt;/p&gt;
&lt;p&gt;
   For example:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   { // Tx0&lt;span style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;
   // No-Tx&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Can we make any statements about the value of x after Tx0 commits (or rolls back)?&amp;nbsp;
   Not really.&amp;nbsp; It depends on the way the particular TM being used has been implemented.&amp;nbsp;
   An in-place model that rolls back could not only roll back Tx0’s but also the unprotected
   x++’s write.&amp;nbsp; And so on.
&lt;/p&gt;
&lt;p&gt;
   On one hand, this code is racey.&amp;nbsp; So you could explain away the undefined behavior
   as being a race condition.&amp;nbsp; On the other hand, it was also troublesome.&amp;nbsp;
   All those problems with locks begin cropping up all over the place.&amp;nbsp; It would
   have been ideal if we could notify developers that they made a mistake.&amp;nbsp; Then
   we could have made the assertion that data races are simply not possible with TM.
&lt;/p&gt;
&lt;p&gt;
   (Except for consistency-related ones, of course.)
&lt;/p&gt;
&lt;p&gt;
   At the same time, many hardware models were being explored.&amp;nbsp; And of course in
   hardware you’ve got the physical addresses that variables resolve to and needn’t worry
   about aliasing.&amp;nbsp; So it was actually possible to issue a fault if a location was
   used transactionally and non-transactionally at once.&amp;nbsp; But given that our solution
   was software-based, we were uncomfortable betting the farm on hardware support.
&lt;/p&gt;
&lt;p&gt;
   Another approach was static analysis.&amp;nbsp; We could require transactional locations
   to be tagged, for example.&amp;nbsp; This had the unfortunate consequence of making reusable
   data structures less, well, reusable.&amp;nbsp; Collections for example presumably need
   to be usable from within and outside transactions alike.&amp;nbsp; After-the-fact analysis
   could be applied without tagging, but false positives were common.&amp;nbsp; We never
   really took a hard stance on this problem, but always assumed the combination of static
   analysis, tooling, and, perhaps someday, hardware detection would make this problem
   more diagnosable.&amp;nbsp; But I think we generally resolved ourselves to the fact that
   our TM would suffer from weak atomicity problems.
&lt;/p&gt;
&lt;p&gt;
   We thought this was explainable.&amp;nbsp; Sadly it led to something that surely was not.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Disillusionment Part III: the Privatization Problem&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   I still remember the day like it was yesterday.&amp;nbsp; A regular weekly team meeting,
   to discuss our project’s status, future, hard problems, and the like.&amp;nbsp; A summer
   intern on board from a university doing pioneering work in TM, sipping his coffee.&amp;nbsp;
   Me, sipping my tea.&amp;nbsp; Then that same intern’s casual statement pointing out an
   Earth-shattering flaw that would threaten the kind of TM we (and most of the industry
   at the time) were building.&amp;nbsp; We had been staring at the problem for over a year
   without having seen it.&amp;nbsp; It is these kinds of moments that frighten me and make
   me a believer in formal computer science.
&lt;/p&gt;
&lt;p&gt;
   Here it is in a nutshell:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;bool
   itIsOwned = false;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;MyObj
   x = new MyObj();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;…&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;atomic
   { // Tx0&lt;span style="mso-tab-count: 4"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;atomic
   { // Tx1&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   Claim the state for my use:&lt;span style="mso-tab-count: 1"&gt; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (!itIsOwned)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;itIsOwned
   = true;&lt;span style="mso-tab-count: 3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x.field
   += 42&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;span style="mso-tab-count: 5"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;int
   z = x.field;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   The Tx0 transaction changes itIsOwned to true, and then commits.&amp;nbsp; After it has
   committed, it proceeds to using whatever state was claimed (in this case an object
   referred to by variable x) outside of the purview of TM.&amp;nbsp; Meanwhile, another
   transaction Tx1 has optimistically read itIsOwned as false, and has gone ahead to
   use x.&amp;nbsp; An update in-place system will allow that transaction to freely change
   the state&amp;nbsp;of x.&amp;nbsp; Of course, it will roll back here, because isItOwned changed
   to&amp;nbsp;true.&amp;nbsp; But by then it is too late: the other thread using x outside of
   a transaction will see constantly changing state&amp;nbsp;– torn reads even –&amp;nbsp;and
   who knows what will happen from there.&amp;nbsp;&amp;nbsp; A known flaw in any weakly atomic,
   update in-place&amp;nbsp;TM.
&lt;/p&gt;
&lt;p&gt;
   If this example appears contrived, it’s not.&amp;nbsp; It shows up in many circumstances.&amp;nbsp;
   The first one in which we noticed it was when one transaction removes a node from
   a linked list, while another transaction is traversing that same list.&amp;nbsp; If the
   former thread believes it “owns” the removed element simply because it took it out
   of the list, someone’s going to be disappointed when its state continues to change.
&lt;/p&gt;
&lt;p&gt;
   This, we realized, is just part and parcel of an optimistic TM system that does in-place
   writes.&amp;nbsp; I don’t know that we ever fully recovered from this blow.&amp;nbsp; It was
   a tough pill to swallow.&amp;nbsp; After that meeting, everything changed: a somber mood
   was present and I think we all needed a drink.&amp;nbsp; Nevertheless we plowed forward.
&lt;/p&gt;
&lt;p&gt;
   We explored a number of alternatives.&amp;nbsp; And so did the industry at large, because
   that intern in question published &lt;a href="http://portal.acm.org/citation.cfm?id=1281161"&gt;a
   paper on the problem&lt;/a&gt;.&amp;nbsp; One obvious solution is to have a transaction that
   commits a change to a particular location wait until all transactions that have possibly
   read that location have completed – a technique we called quiescence.&amp;nbsp; We experimented
   with this approach, but it was extraordinarily complicated, for obvious reasons.
&lt;/p&gt;
&lt;p&gt;
   We experimented with blends of pessimistic operations instead of optimistic, alternative
   commit protocols, like using a “commit ticket” approach that serializes transaction
   commits, each of which tended to sacrifice performance greatly.&amp;nbsp; Eventually the
   team decided to do buffered writes instead of in-place writes, because any concurrent
   modifications in a transaction will simply not modify the actual memory being used
   outside of the transaction unless that transaction successfully commits.
&lt;/p&gt;
&lt;p&gt;
   This, however, led to still other problems, like the granular loss of atomicity problem.&amp;nbsp;
   Depending on the granularity of your buffered writes – we chose object-level – you
   can end up with false sharing of memory locations between transactional and non-transactional
   code.&amp;nbsp; Imagine you update two separate fields of an object from within and outside
   a transaction, respectively, concurrently.&amp;nbsp; Is this legal?&amp;nbsp; Perhaps not.&amp;nbsp;
   The transaction may bundle state updates to the whole object, rather than just one
   field.
&lt;/p&gt;
&lt;p&gt;
   All these snags led to the realization that we direly needed a memory model for TM.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Disillusionment Part IV: Where is the Killer App?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   Throughout all of this, we searched and searched for the killer TM app.&amp;nbsp; It’s
   unfair to pin this on TM, because the industry as a whole still searches for a killer
   concurrency app.&amp;nbsp; But as we uncovered more successes in the latter, I became
   less and less convinced that the killer concurrency apps we will see broadly deployed
   in the next 5 years needed TM.&amp;nbsp; Most enjoyed natural isolation, like embarrassingly
   parallel image processing apps.&amp;nbsp; If you had sharing, you were doing something
   wrong.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;In Conclusion&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
   I eventually shifted focus to enforcing coarse-grained isolation through message-passing,
   and fine-grained isolation through type system support a la Haskell’s state monad.&amp;nbsp;
   This would help programmers to realize where they accidentally had sharing, I thought,
   rather than merely masking this sharing and making it all work (albeit inefficiently).
&lt;/p&gt;
&lt;p&gt;
   I took this path not because I thought TM had no place in the concurrency ecosystem.&amp;nbsp;
   But rather because I believed it did have a place, but that several steps would be
   needed before getting there.
&lt;/p&gt;
&lt;p&gt;
   I suspected that, just like with Argus, you’d want transactions around the boundaries.&amp;nbsp;
   And that you’d probably want something like open nesting for fine-grained scalable
   data structures, like shared caches.&amp;nbsp; These are often choke points in a coarse-grained
   locking system, and often cannot be fully isolated, at least in the small.&amp;nbsp; Ironically
   I am just now arriving there.&amp;nbsp; In the system I work on I see these issues actually
   staring us in the face.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
   This is just my own personal view on TM.&amp;nbsp; You may also be interested in reading
   the current STM.NET team’s views also, available on &lt;a href="http://blogs.msdn.com/stmteam/"&gt;their
   MSDN blog&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   For me the TM project was particularly enjoyable.&amp;nbsp; And it was a great learning
   experience.&amp;nbsp; I worked with some amazing people, and it was a privilege.&amp;nbsp;
   You really had the sense that something big was right around the corner, and every
   day was a rush of enjoyment.&amp;nbsp; Despite running as fast as we could, it seemed
   like we could just barely keep pace with the research community.&amp;nbsp; Over time more
   and more researchers turned to TM, and I distinctly recall reading at least one new
   TM paper per week.
&lt;/p&gt;
&lt;p&gt;
   This was also the first time I realized that Microsoft, at its core, really does operate
   like a collection of many startups.&amp;nbsp; Our TM work was a grassroots movement, and
   there was no official sponsorship for our effort at the start.&amp;nbsp; It was just a
   group of people independently getting together to discuss how TM might fit into the
   direction the industry was headed.&amp;nbsp; Eventually TM started showing up on slide
   decks in presentations to management, followed by dedicated TM reviews, and even a
   BillG review.&amp;nbsp; I will never forget, a couple years after that review – during
   an overall concurrency review – Bill standing up at the whiteboard, drawing the code
   “atomic { … }” and asking something to the effect: “Why can’t you just use transactional
   memory for that?”&amp;nbsp; I guess the idea stuck with him too.
&lt;/p&gt;
&lt;p&gt;
   Who knows.&amp;nbsp; Maybe in 10 years, the world will be transactional after all.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=4cfad7df-ed84-46a4-a961-54aafdaeb9d7" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,4cfad7df-ed84-46a4-a961-54aafdaeb9d7.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=9391c3c8-4935-476d-b2a6-bf7e4374ee47</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,9391c3c8-4935-476d-b2a6-bf7e4374ee47.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,9391c3c8-4935-476d-b2a6-bf7e4374ee47.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=9391c3c8-4935-476d-b2a6-bf7e4374ee47</wfw:commentRss>
      <slash:comments>20</slash:comments>
      <title>Lifting T out of Task&lt;T&gt; with dynamic dispatch</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,9391c3c8-4935-476d-b2a6-bf7e4374ee47.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/11/01/LiftingTOutOfTaskTWithDynamicDispatch.aspx</link>
      <pubDate>Sun, 01 Nov 2009 21:49:28 GMT</pubDate>
      <description>&lt;p&gt;
   Say you've got a Task&amp;lt;T&amp;gt;.&amp;nbsp; Well, now what?
&lt;/p&gt;
&lt;p&gt;
   You know that eventually a T will become available, but until then you're out of luck.&amp;nbsp;
   You could go ahead and be a naughty little devil by calling Wait on it -- blocking
   the current thread (eek!) -- or you could call ContinueWith on the task to get back
   a new Task&amp;lt;U&amp;gt;, representing the work you &lt;em&gt;would&lt;/em&gt; do to create some new
   U object if only you presently had a T in hand.&amp;nbsp; And then perhaps you will find
   yourself in the same situation for that U.
&lt;/p&gt;
&lt;p&gt;
   These are those dataflow graphs I mentioned in the previous blog post.&amp;nbsp; Things
   of beauty.
&lt;/p&gt;
&lt;p&gt;
   To be more concrete about the situation I describe, imagine you've got the following
   IFoo interface:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;interface
   IFoo&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   Bar();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;string
   Baz(int x);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Now, given a Task&amp;lt;IFoo&amp;gt;, you can't do anything related to an IFoo.&amp;nbsp; And
   yet presumably that's why you've got the task in the first place: because you care
   about the IFoo.&amp;nbsp; What if you ultimately want to invoke the Bar method, for example?
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;IFoo&amp;gt;
   task = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
   You can of course block the thread:
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//
   Option A: block the thread.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;int
   resultA = task.Result.Bar();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Or you can choose to program in a very clunky way:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//
   Option B: use dataflow.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   resultB = task.ContinueWith(t =&amp;gt; t.Result.Bar());&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   But what if, instead, you could do something like this?
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//
   Option C: magic.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   resultC = task.Bar();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Whoa, wait a minute.&amp;nbsp; We're calling Bar() on a Task&amp;lt;IFoo&amp;gt;?&amp;nbsp; Neat,
   but how can that be?
&lt;/p&gt;
&lt;p&gt;
   This is obviously a trick.&amp;nbsp; All of the members of T are somehow being made available
   on the Task&amp;lt;T&amp;gt; object, so that they can be called before the task has actually
   been resolved to a concrete value.&amp;nbsp; Of course, were we to allow this, what you
   get back to represent the result of such calls would need to be task objects too:
   hence we get back a Task&amp;lt;int&amp;gt; from the call on Bar(), instead of an int.&amp;nbsp;
   This is similar to call streams in Barbara Liskov's Argus language (her primary focus
   immediately after CLU).
&lt;/p&gt;
&lt;p&gt;
   This kind of lifting from the inner type outward is much like what you get in languages
   that allow generic mixins.&amp;nbsp; C# already has one semi-such type, though you may
   not realize it: Nullable&amp;lt;T&amp;gt; actually allows you to directly access interfaces
   implemented by T without needing to call Value on it.&amp;nbsp; It's almost like Nullable&amp;lt;T&amp;gt;
   was defined as deriving from T itself which is clearly not actually possible (for
   numerous reasons, not the least significant of which is that it's a struct).&amp;nbsp;
   Try it.&amp;nbsp; This works because the type system treats Nullable&amp;lt;T&amp;gt; and T somewhat
   uniformly (though you'd be surprised by some dangers lurking within -- effectively
   Nullable&amp;lt;T&amp;gt; mustn't implement any interfaces *ever* otherwise a type hole would
   result).&amp;nbsp; But I digress...
&lt;/p&gt;
&lt;p&gt;
   Unfortunately without deep language changes we can't get this to work the way we'd
   like.&amp;nbsp; I have found numerous occasions where a general lifting capability in
   C# would be useful: Lazy&amp;lt;T&amp;gt; is but one example.&amp;nbsp; That said, each time we
   run across an instance, it demands slightly different type system treatment, and it
   seems unlikely such a general facility would be as usable as the one off features.
&lt;/p&gt;
&lt;p&gt;
   Type systems aside, I am actually using a very dirty trick to make this work: I'm
   using the new System.Dynamic features in .NET 4.0 to do it all dynamically.&amp;nbsp;
   You may love or hate this, depending on your stance on type systems.&amp;nbsp; Being an
   ML guy, I'll let you figure out what I think.&amp;nbsp; (Hint: gross hack!)
&lt;/p&gt;
&lt;p&gt;
   We can go further.&amp;nbsp; (Although sadly I won't demonstrate how to do so in this
   blog post.&amp;nbsp; I had wanted to go all the way, but need to get some actual language
   work done today, in addition to a little Riemann study, instead of having endless
   fun tinkering with Visual Studio 2010.&amp;nbsp; Shucks.)&amp;nbsp; Notice that Baz accepts
   an int as input.&amp;nbsp; Well, what if all we've got is a Task&amp;lt;int&amp;gt;?&amp;nbsp; We
   can of course also allow that to get passed in too:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;string&amp;gt;
   resultD = task.Baz(42); // Real input.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Fine.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   arg = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;string&amp;gt;
   resultE = task.Baz(arg); // A task as input!&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Cool!&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   But wait, there is more!&amp;nbsp; It slices and dices too.&amp;nbsp; The next trick is difficult
   -- if not impossible -- to do without far reaching language changes.&amp;nbsp; But we
   could also even bridge the world of ordinary methods too, not just those that have
   been accessed by tunneling through a Task&amp;lt;T&amp;gt;.&amp;nbsp; For example:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;string
   f(int x) {...}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   task = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;string&amp;gt;
   result = f(task);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
   Not to even mention:
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   x = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   y = ...;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
   z = x + y;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;div&gt;
   &lt;p&gt;
      This is deep.&amp;nbsp; What we are saying is that anywhere a T is expected, we can supply
      a Task&amp;lt;T&amp;gt;.&amp;nbsp; Of course once we've entered the world of tasks, we cannot
      escape until values actually begin resolving.&amp;nbsp; So when we invoke the method f
      in this example, we of course get back a Task&amp;lt;string&amp;gt; for its result.&amp;nbsp;
      Once we've stepped onto a turtle's back, well, it's turtles all the way down.
   &lt;/p&gt;
   &lt;p&gt;
      (Which reminds me of the well known tale:
   &lt;/p&gt;
   &lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
   &lt;p&gt;
      &lt;em&gt;A well-known scientist (some say it was Bertrand Russell) once gave a public lecture
      on astronomy. He described how the earth orbits around the sun and how the sun, in
      turn, orbits around the center of a vast collection of stars called our galaxy. At
      the end of the lecture, a little old lady at the back of the room got up and said:
      "What you have told us is rubbish. The world is really a flat plate supported on the
      back of a giant tortoise." The scientist gave a superior smile before replying, "What
      is the tortoise standing on?" "You're very clever, young man, very clever", said the
      old lady. "But it's turtles all the way down!"&lt;/em&gt;
   &lt;/p&gt;
   &lt;/blockquote&gt; 
   &lt;p&gt;
      Tasks are not greasy hamburgers after all, as I had claimed in the last post, but
      rather they are turtles.
   &lt;/p&gt;
   &lt;p&gt;
      I've wasted all of my energy speaking of turtle hamburgers drenched in asynchronous
      aioli, and have left only a little to go over the hacked up implementation of this
      idea.&amp;nbsp; Sigh.&amp;nbsp; Well, we had better get to it.)
   &lt;/p&gt;
   &lt;p&gt;
      In summary: we'll just rely on dynamic dispatch to do the lifting, thanks to the new
      .NET 4.0 DynamicObject class.&amp;nbsp; This is wildly less efficient than a proper type
      system design would yield, not to mention the utter lack of static type checking.&amp;nbsp;
      Of course a proper implementation that designed for this from Day One would also avoid
      the tremendous amount of object allocation that relying on the current Task&amp;lt;T&amp;gt;
      objects and ContinueWith overloads imply.&amp;nbsp; But nevertheless, this approach will
      allow us to at least have a good ole' time and stimulate the creative side of the
      noggin.
   &lt;/p&gt;
   &lt;p&gt;
      First, I shall provide an extension method for getting a DynamicTask&amp;lt;T&amp;gt; -- the
      thing that actually derives from DynamicObject and implements the custom dynamic binding:
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;public
      static class DynamicTask&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      static dynamic AsDynamic&amp;lt;T&amp;gt;(this Task&amp;lt;T&amp;gt; task)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      new DynamicTask&amp;lt;T&amp;gt;(task);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      Notice that this changes our calling conventions ever so slightly.&amp;nbsp; Namely:
   &lt;/p&gt;
   &lt;p&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;//
      Option C: magic.&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Task&amp;lt;int&amp;gt;
      resultC = task.AsDynamic().Bar();&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      The AsDynamic places the caller into the lifted context.&amp;nbsp; As invocations are
      made, the results become real tasks, and not dynamic ones, such that to continue the
      calling will require many AsDynamic()s.&amp;nbsp; This is a minor inconvenience and we
      could certainly automatically wrap the return values in DynamicTask&amp;lt;T&amp;gt; objects
      if we wanted to eliminate this problem, i.e. to make chaining less verbose.
   &lt;/p&gt;
   &lt;p&gt;
      Second, we must implement the DynamicTask&amp;lt;T&amp;gt; class.&amp;nbsp; We will do a very
      simple translation.&amp;nbsp; Given a member access expression 'x.m', where m is either
      a field or property of type U, we will morph this into the new expression 'x.Task.ContinueWith(v
      =&amp;gt; v.Result.m)', which is of type Task&amp;lt;U&amp;gt;.&amp;nbsp; Similarly, given a method
      invocation 'x.M(a1,...,aN)', whose return value is of type U, we will morph it into
      the new expression 'x.Task.ContinueWith(v =&amp;gt; v.Result.M(a1,...,aN))', which is
      of type Task&amp;lt;U&amp;gt; (or just Task if U is the void type).&amp;nbsp; To support the ability
      to pass a task argument where an actual one is expected would require packing the
      argument with the target into an array, and doing a ContinueWhenAll on it.
   &lt;/p&gt;
   &lt;p&gt;
      (Perhaps I will illustrate how to do these other tricks in a later post, but I'm tight
      for time right now.&amp;nbsp; I'm only sketching the general idea.&amp;nbsp; Even in what
      I show below, things will be incomplete, because topics such as getting exception
      propagation right when tasks begin failing are tricky.&amp;nbsp; Ideally the whole dataflow
      chain will be "broken" by such an exception.&amp;nbsp; Additionally, I've only implemented
      what was necessary to get a few interesting examples working.&amp;nbsp; The binder, for
      example, certainly has a few loose ends.&amp;nbsp; Blog reader beware.)
   &lt;/p&gt;
   &lt;p&gt;
      Here is the implementation of DynamicTask&amp;lt;T&amp;gt;:
   &lt;/p&gt;
   &lt;p&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;public
      class DynamicTask&amp;lt;T&amp;gt; : DynamicObject&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      Task&amp;lt;T&amp;gt; m_task;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      DynamicTask(Task&amp;lt;T&amp;gt; task)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (task == null) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
      new ArgumentNullException("task");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_task
      = task;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      Task&amp;lt;T&amp;gt; Task {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;get
      { return m_task; }&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      override DynamicMetaObject GetMetaObject(Expression parameter) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (parameter == null) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
      new Exception("parameter");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      new TaskLiftedObject(this, parameter);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;class
      TaskLiftedObject : DynamicMetaObject&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      Simple.&amp;nbsp; All of the dynamic magic resides in the implementation of TaskLiftedObject,
      which derives from the DynamicMetaObject class.&amp;nbsp; It is constructed with an instance
      of the DynamicTask&amp;lt;T&amp;gt; along with the expression tree that can be used to dynamically
      load up an instance of that task.&amp;nbsp; All of the dynamic features work with expression
      trees.&amp;nbsp; For example, in response to an attempt to invoke a method M on a DynamicTask&amp;lt;T&amp;gt;,
      our binder will need to find the right method M on the underlying T, and then return
      an expression tree that does the ContinueWith and so forth.
   &lt;/p&gt;
   &lt;p&gt;
      Let's start cracking open TaskLiftedObject:
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;class
      TaskLiftedObject : DynamicMetaObject&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      DynamicTask&amp;lt;T&amp;gt; m_task;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      TaskLiftedObject(DynamicTask&amp;lt;T&amp;gt; task, Expression expression) :&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;base(expression,
      BindingRestrictions.Empty, task)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m_task
      = task;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      We will override two of DynamicMetaObject's functions.&amp;nbsp; BindGetMember is called
      when a member is accessed (like a property or field), whereas BindInvokeMember is
      called when a method call is made.&amp;nbsp; There are several other methods that a proper
      binder would need to override in order to make delegate dispatch and such work properly.&amp;nbsp;
      But this suffices to get started:
   &lt;/p&gt;
   &gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      override DynamicMetaObject BindGetMember(GetMemberBinder binder)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      We have a member access:&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x.m&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      which must become:&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x.Task.ContinueWith(v
      =&amp;gt; { v.Result.m; })&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      new DynamicMetaObject(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;MakeContinuationTask(Bind(binder.Name,
      -1), null),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BindingRestrictions.GetInstanceRestriction(Expression,
      Value),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Value&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
      override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[]
      args)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      We have a call:&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x.Foo(a1,...,aN)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      which must become:&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x.Task.ContinueWith(v
      =&amp;gt; { v.Result.Foo(a1,...,aN); })&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression[]
      argsEx = new Expression[args.Length];&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
      (int i = 0; i &amp;lt; args.Length; i++) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;argsEx[i]
      = args[i].Expression;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      new DynamicMetaObject(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MakeContinuationTask(Bind(binder.Name,
      binder.CallInfo.ArgumentCount), argsEx),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BindingRestrictions.GetInstanceRestriction(Expression,
      Value),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Value&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      Clearly the workhorses here are Bind and MakeContinuationTask.&amp;nbsp; Bind is responsible
      for performing dynamic lookup for a matching member on T that has the requested Name
      and, if a method call is being made, the proper number of parameters.&amp;nbsp; For brevity,
      I've omitted anything to do with argument type checking, an obvious hole that we'd
      want to fix some day:
   &lt;/p&gt;
   &gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      static MemberInfo Bind(string name, int argCount)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      Lookup the target member on the T, rather than the (Dynamic)Task&amp;lt;T&amp;gt;.&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(from
      m in typeof(T).GetMembers(BindingFlags.Instance | BindingFlags.Public)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;where
      m.Name.Equals(name) &amp;amp;&amp;amp;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;(argCount
      == -1 ?&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;!(m
      is MethodInfo) :&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;((MethodInfo)m).GetParameters().Length
      == argCount)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;select
      m).&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Single();&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      Nothing too interesting here either -- just a bit of hacky reflection code done with
      a fancy LINQ query.&amp;nbsp; If anything other than exactly one method was found, the
      call to Single() will throw an exception.&amp;nbsp; If you want to see what a "real" dynamic
      binder looks like, you won't find it here: check out VB's or IronPython's.
   &lt;/p&gt;
   &lt;p&gt;
      Now for the meat.&amp;nbsp; The MakeContinuationTask method takes the target member that
      we've found dynamically via Bind, as well as an optional array of expression trees,
      each representing an argument being passed to the target method (and which will be
      null for property and field access), and manufactures the expression tree that represents
      the execution of the dynamic call itself:
   &lt;/p&gt;
   &gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      Expression MakeContinuationTask(MemberInfo target, Expression[] targetArgs)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
      lambdaParam = Expression.Parameter(typeof(Task&amp;lt;T&amp;gt;), "v");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
      lambdaParamResult = Expression.Property(lambdaParam, "Result");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression
      lambdaBody;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Type
      lambdaReturnType;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (target is MethodInfo) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaBody
      = Expression.Call(lambdaParamResult, (MethodInfo)target, targetArgs);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaReturnType
      = ((MethodInfo)target).ReturnParameter.ParameterType;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
      if (target is PropertyInfo) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaBody
      = Expression.Property(lambdaParamResult, (PropertyInfo)target);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaReturnType
      = ((PropertyInfo)target).PropertyType;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
      if (target is FieldInfo) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaBody
      = Expression.Field(lambdaParamResult, (FieldInfo)target);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambdaReturnType
      = ((FieldInfo)target).FieldType;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
      {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
      new Exception("Unsupported dynamic invoke: " + target.GetType().Name);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      Expression.Call(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression.Property(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression.Convert(this.Expression,
      typeof(DynamicTask&amp;lt;T&amp;gt;)),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;typeof(DynamicTask&amp;lt;T&amp;gt;).GetProperty("Task")&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;),&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;GetContinueWith(lambdaReturnType),
      // ContinueWith&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;new
      Expression[] {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      v =&amp;gt; { v.Result.M(a0,...,aN) }&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Expression.Lambda(lambdaBody,
      lambdaParam)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      You should be able to convince yourself that this code generates the desired transformation
      described earlier.&amp;nbsp; It uses a method to find the overload of Task&amp;lt;T&amp;gt;.ContinueWith
      that we want to bind against, and invokes that on the Task&amp;lt;T&amp;gt; contained within
      the DynamicTask&amp;lt;T&amp;gt; against which the dynamic call was made.&amp;nbsp; It is rather
      unfortunate that the CLR does not allow the void type as a generic type argument,
      so we have to be a little bit inconsistent with our treatment of void returns, by
      choosing a different ContinueWith overload.
   &lt;/p&gt;
   &lt;p&gt;
      If the above reflection code was called hacky, the ContinueWith lookup is worse.&amp;nbsp;
      It's very inefficient, not to mention fragile (because it depends on the current layout
      of Task&amp;lt;T&amp;gt;'s overloads, what with instantiating generic methods and the like).&amp;nbsp;
      C'est la vie:
   &lt;/p&gt;
   &gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private
      static MethodInfo GetContinueWith(Type returnType)&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
      @TODO: caching to avoid expensive lookups each time.&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (returnType == typeof(void)) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      typeof(Task&amp;lt;T&amp;gt;).GetMethod(&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;"ContinueWith",&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;new
      Type[] { typeof(Action&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt;) }&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
      {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;foreach
      (MethodInfo mif in typeof(Task&amp;lt;T&amp;gt;).GetMethods()) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (mif.Name == "ContinueWith" &amp;amp;&amp;amp; mif.IsGenericMethodDefinition) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MethodInfo
      mifOfT = mif.MakeGenericMethod(returnType);&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ParameterInfo[]
      mifParams = mifOfT.GetParameters();&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
      (mifParams.Length == 1 &amp;amp;&amp;amp;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;mifParams[0].ParameterType
      == typeof(Func&amp;lt;,&amp;gt;).MakeGenericType(typeof(Task&amp;lt;T&amp;gt;), returnType)) {&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
      mifOfT;&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
      &lt;o:p&gt;
         &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
      &lt;/o:p&gt;
      &lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw
      new Exception("Fatal error: ContinueWith overload not found");&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
      &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
      &lt;/font&gt;&lt;/span&gt;
   &lt;/p&gt;
   &lt;p&gt;
   &lt;p&gt;
      And that's it.&amp;nbsp; With that, we can get dynamic invocations on unresolved T's via
      Task&amp;lt;T&amp;gt; objects.&amp;nbsp; Nifty.
   &lt;/p&gt;
   &lt;p&gt;
      I'm not saying any of this is a really good idea.&amp;nbsp; Honestly, I'm not.&amp;nbsp; Of
      course, there's a kernel of a good idea there and the systems we are working on take
      this kernel to its extreme.&amp;nbsp; By providing a programming model that encourages
      deep chains of datafow to be expressed speculatively in a natural and familiar manner,
      greater degrees of latent parallelism can lie resident in an application waiting to
      be unlocked as more processors become available.&amp;nbsp; Doing it for real requires
      impactful changes to the language, supporting infrastructure, and particularly tooling.&amp;nbsp;
      Just imagine what it means to break into a debugger to inspect deep dataflow graphs
      that have been constructed by compiler magic underneath you.&amp;nbsp; And the use of
      ContinueWith is a little lame, because of course the target of our call may be something
      that can be run speculatively too with first class pipleining, rather than completely
      delaying the invocation of it.
   &lt;/p&gt;
   &lt;p&gt;
      So we won't be seeing lifted tasks in .NET anytime soon.&amp;nbsp; Writing up this blog
      post was merely an excuse to toy around with the new C# dynamic features and to have
      a little recreational time.&amp;nbsp;&amp;nbsp; And to generate excitement about what .NET
      4.0 holds in store.&amp;nbsp; I hope you have enjoyed it.&amp;nbsp; Now back to reality.
   &lt;/p&gt;
   &gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=9391c3c8-4935-476d-b2a6-bf7e4374ee47" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,9391c3c8-4935-476d-b2a6-bf7e4374ee47.aspx</comments>
      <category>Technology</category>
    </item>
    <item>
      <trackback:ping>http://www.bluebytesoftware.com/blog/Trackback.aspx?guid=a8cbba20-d3d3-44e5-9b3e-896387803a44</trackback:ping>
      <pingback:server>http://www.bluebytesoftware.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.bluebytesoftware.com/blog/PermaLink,guid,a8cbba20-d3d3-44e5-9b3e-896387803a44.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.bluebytesoftware.com/blog/CommentView,guid,a8cbba20-d3d3-44e5-9b3e-896387803a44.aspx</wfw:comment>
      <wfw:commentRss>http://www.bluebytesoftware.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=a8cbba20-d3d3-44e5-9b3e-896387803a44</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>Tasks and asynchronous control flow</title>
      <guid>http://www.bluebytesoftware.com/blog/PermaLink,guid,a8cbba20-d3d3-44e5-9b3e-896387803a44.aspx</guid>
      <link>http://www.bluebytesoftware.com/blog/2009/11/01/TasksAndAsynchronousControlFlow.aspx</link>
      <pubDate>Sun, 01 Nov 2009 04:06:24 GMT</pubDate>
      <description>&lt;p&gt;
   Well, Visual Studio 2010 Beta 2 is &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;out
   on the street&lt;/a&gt;.&amp;nbsp; It contains plenty of neat new things to keep one busy for
   at least a rainy Saturday.&amp;nbsp; I proved this today.
&lt;/p&gt;
&lt;p&gt;
   Of course, Parallel Extensions is in the box.&amp;nbsp; .NET 4.0's Task and Task&amp;lt;T&amp;gt;
   abstractions are used&amp;nbsp;to implement such things as PLINQ and Parallel.For loops,
   but of course they are&amp;nbsp;great for representing asynchronous work too.&amp;nbsp; The
   FromAsync adapters move you from the dark ages of IAsyncResult to the glitzy new space
   age of tasks.
&lt;/p&gt;
&lt;p&gt;
   Not only are tasks tastier than hamburgers, but they enable complex dataflow graphs
   of asynchronous work to unfold dynamically at runtime, thanks to the ContinueWith
   method.&amp;nbsp; From a Task&amp;lt;T&amp;gt; you can get a Task&amp;lt;U&amp;gt;&amp;nbsp;that was computed
   based on the T; ad infinitum.&amp;nbsp;&amp;nbsp;We like dataflow.&amp;nbsp; It is the key to
   unlocking parallelism, or more accurately,&amp;nbsp;boiling away all else &lt;em&gt;except for&lt;/em&gt; dataflow&amp;nbsp;is
   the key.&amp;nbsp; But what about control flow, you might ask?&amp;nbsp; We like it less.&amp;nbsp;
   But you can do it, so long as you put in some work.&amp;nbsp; F#'s async workflows make
   this sort of thing a tad easier, but the raw libraries in .NET 4.0 don't come with
   any sort of loops or conditional capabilities.&amp;nbsp; Perhaps in the future they will.&amp;nbsp;
   Nevertheless, in this post I shall demonstrate how to build a couple simple ones.
&lt;/p&gt;
&lt;p&gt;
   Not because the lack of them is going to cause unprecidented and unheard of horrors,
   but rather because in doing so we'll see some neat features of tasks.
&lt;/p&gt;
&lt;p&gt;
   The two methods I will illustrate in this post are:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;public
   static class TaskControlFlow&lt;/span&gt;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;{&lt;/span&gt;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;public
   static Task For(int from, int to, Func&amp;lt;int, Task&amp;gt; body, int width)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public
   static Task While(Func&amp;lt;int, bool&amp;gt; condition, Func&amp;lt;int, Task&amp;gt; body, int
   width)&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Notice that each body is given the iteration index and is expected to launch asynchronous
   work and return a Task.&amp;nbsp; The parameters that these methods take are probably
   obvious.&amp;nbsp; Well, except for the last one.&amp;nbsp; The "width" indicates how many
   outstanding asynchronous bodies should be in flight at once.&amp;nbsp; The Task returned
   by For and While won't be considered done until all iterations are done, and any exceptions
   will be propagated as you might hope.&amp;nbsp; It would be pretty useless otherwise.
&lt;/p&gt;
&lt;p&gt;
   For example, we could write a while loop that does something very silly:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;TaskControlFlow.While(&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;i
   =&amp;gt; i &amp;lt; 100,&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;i
   =&amp;gt; { return CreateTimerTask(250).ContinueWith(_ =&amp;gt; Console.WriteLine(i)); },&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;4&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;).Wait();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   This just prints returns a "timer task" that completes after 250ms and prints out
   the iteration to the console. We pass a width of 4, so only four tasks will be outstanding
   at any given time.&amp;nbsp; Notice we call Wait at the end, since both For and While
   return tasks representing the in flight work.&amp;nbsp; This could have instead been written
   using a For loop as follows:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;TaskControlFlow.For(0,
   100,&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;i
   =&amp;gt; { return CreateTimerTask(250).ContinueWith(_ =&amp;gt; Console.WriteLine(i)); },&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;4&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;).Wait();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   The CreateTimerTask method, by the way, looks like this:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;private
   static Task CreateTimerTask(int ms)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
   tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;new
   Timer(x =&amp;gt; ((TaskCompletionSource&amp;lt;bool&amp;gt;)x).SetResult(true), tcs, ms, -1);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;return
   tcs.Task;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   As something more realistic, imagine we wanted to do something with a large number
   of files, and don't want to block a whole bunch of threads in the process.&amp;nbsp; The
   following "simple" expression will count up all of the bytes for all of the files
   in a particular directory, without once blocking the thread -- well, except for the
   initial call to Directory.GetFiles:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;string
   win = "c:\\...\\";&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;string[]
   files = Directory.GetFiles(win);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;int
   total = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;TaskControlFlow.For(0,
   files.Length,&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   i =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bool
   eof = false;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int
   offset = 0;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;byte[]
   buff = new byte[4096];&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;FileStream
   fs = File.OpenRead(files[i]);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;return
   TaskControlFlow.While(&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;j
   =&amp;gt; !eof,&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;j
   =&amp;gt; Task&amp;lt;int&amp;gt;.Factory.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FromAsync&amp;lt;byte[],int,int&amp;gt;(&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   fs.BeginRead, fs.EndRead, buff, offset, buff.Length,&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;null,
   TaskCreationOptions.None&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;).&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;ContinueWith(v
   =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (eof = v.Result &amp;lt; buff.Length) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;fs.Close();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;offset
   += v.Result;&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/span&gt;Interlocked.Add(ref total, v.Result);&lt;/font&gt;&lt;/span&gt;&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}),&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;1&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;},&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;8&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;).Wait();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;Console.WriteLine(total);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   Pretty neat.&amp;nbsp; We've somewhat arbitrarily chosen a width of 8 for this loop.&amp;nbsp;
   And notice something very subtle but important here: we've chosen a width of 1 for
   the inner loop that plows through the bytes of a file.&amp;nbsp; This is because we're
   sharing state, and it would not be safe to launch numerous iterations at once.&amp;nbsp;
   The same byte[], eof variable, and so forth, would become corrupt.&amp;nbsp; I will mention
   in passing that it's unfortunate that we've got that interlocked stuck in there to
   add to the total.&amp;nbsp; Refactoring this so that we could just do a LINQ reduce over
   the whole thing would be nice.&amp;nbsp; Indeed, it can be done.
&lt;/p&gt;
&lt;p&gt;
   We can do away with the For implementation very quickly.&amp;nbsp; It is just implemented
   in terms of While:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;public
   static Task For(int from, int to, Func&amp;lt;int, Task&amp;gt; body, int width)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   While(i =&amp;gt; from + i &amp;lt; to, body, width);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   And it turns out that the While implementation is not terribly complicated either.&amp;nbsp;
   Here it is:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;public
   static Task While(Func&amp;lt;int, bool&amp;gt; condition, Func&amp;lt;int, Task&amp;gt; body, int
   width)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;var
   tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int
   currIx = -1; // Current shared index.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int
   currCount = width; // The number of outstanding tasks.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int
   canceled = 0; // 1 if at least one body was cancelled.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;ConcurrentBag&amp;lt;Exception&amp;gt;
   exceptions = null; // A collection of exceptions, if any.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;//
   Generate a continuation action: this fires for each body that completes.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Action&amp;lt;Task&amp;gt;
   fcont = null;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;fcont
   = tsk =&amp;gt; {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;if
   (tsk.IsFaulted) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   Accumulate exceptions.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;LazyInitializer.EnsureInitialized(ref
   exceptions);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;foreach
   (Exception inner in tsk.Exception.InnerExceptions) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;exceptions.Add(inner);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
   if (tsk.IsCanceled) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Mark that cancellation has occurred.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;canceled
   =&amp;nbsp;1;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
   if (canceled == 0 &amp;amp;&amp;amp; exceptions == null) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;//
   If no cancellations / exceptions are found, attempt to kick off more work.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int
   ix = Interlocked.Increment(ref currIx);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (condition(ix)) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   Generate a new body task, handling exceptions.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Then
   make sure we&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   tack on the continuation on that new task, so we can keep on going...&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   If the condition yielded 'false', we'll simply fall through and try to finish.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task
   btsk;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;btsk
   = body(ix);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch
   (Exception ex) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;btsk
   = AlreadyFaulted(ex);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;btsk.ContinueWith(fcont);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   If this is the last task, signal completion.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if
   (Interlocked.Decrement(ref currCount) == 0) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;if
   (exceptions != null) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetException(exceptions);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
   if (canceled == 1) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetCanceled();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else
   {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetResult(true);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//
   Fire off the right number of starting tasks.&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for
   (int i = 0; i &amp;lt; width; i++) {&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AlreadyDone.ContinueWith(fcont);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   tcs.Task;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   I've commented the code inline to illustrate what is going on.&amp;nbsp; The only other
   part that isn't shown are the AlreadyDone and AlreadyFaulted members, which simply
   give Tasks that are already in a final state.&amp;nbsp; This isn't strictly necessary,
   but come in handy in a number of situations:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;internal
   static Task AlreadyDone;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;static
   TaskControlFlow()&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
   tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetResult(true);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AlreadyDone
   = tcs.Task;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;
   &lt;o:p&gt;
      &lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
   &lt;/o:p&gt;
   &lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;private
   static Task AlreadyFaulted(Exception ex)&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;var
   tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;tcs.SetException(ex);&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return
   tcs.Task;&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;
   &lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; mso-bidi-font-family: Tahoma"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
   &lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
   And that's it.&amp;nbsp; I'm done for now.&amp;nbsp; Hope you enjoyed it.&amp;nbsp; I've got a
   few other posts in the works --&amp;nbsp;primarily the result of a day full of hacking
   (I got in the office at 7am this morning, and have been here ever since, 14 hours
   later) --&amp;nbsp;demonstrating how to do speculative asynchronous work for if/else branches.&amp;nbsp;
   Finally, I also have a neat example that illustrates how to do deep dataflow-based
   speculation without having to wait for work to complete.&amp;nbsp; This combines the new
   .NET 4.0 dynamic capabilities with parallelism, so I'm pretty excited to get it working
   and write about it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.bluebytesoftware.com/blog/aggbug.ashx?id=a8cbba20-d3d3-44e5-9b3e-896387803a44" /&gt;</description>
      <comments>http://www.bluebytesoftware.com/blog/CommentView,guid,a8cbba20-d3d3-44e5-9b3e-896387803a44.aspx</comments>
      <category>Technology</category>
    </item>
  </channel>
</rss>