Expression tree closure vs. anonymous delegate closure

Answered Expression tree closure vs. anonymous delegate closure

  • Sunday, June 17, 2007 4:59 PM
     
     

     

    Is there a specific reason expression trees uses R-value for closure unlike anonymous delegates use of L-value?

     

    Code Snippet

    static void Main(string[] args)

    {

       int i = 1;

       Func<int, int> f = j => i + j; // The "closure object" contains the ref. to i

       Expression<Func<int, int>> e = k => i + k; // i is evaluated as the constant 1

       i = 2;

       var v1 = f(40); // v1 = 42

       var v2 = e.Compile()(40); //v2 = 41

    }

     

    Seems like you take away all the fun we are having with anonymous delegates. 

     

    //Jens

All Replies

  • Sunday, June 17, 2007 10:21 PM
     
     Answered
    Expression trees used R-value in the prototype compiler only. The production compiler available in Beta 1 of Orcas uses L-value. Are you seeing otherwise?
  • Monday, June 18, 2007 6:56 PM
     
     

    No, your correct, L-value it is. I wasn't up to date. I'm now and that's nice.

    Before I dig down into the details of the closure machinery you might tell me if there are any major difference between the anonymous delegate closure and the "new" closure for expression trees since statement blocks and assignment operators are not allowed? 

     

    Cheers

    Jens