views:

205

answers:

3

I'm adding a LINQ interface to some custom objects, but the C# compiler fails on type inference. However, I can write the equivalent query using the raw extension methods and type inference succeeds, so I'm not sure how the compiler is translating the query expression into extension method calls.

Is there a tool or compiler flag so I can view what the compiler is generating from my query expression so I figure this out?

This code is in an open source project, so I can provide links to source if that's helpful. Slight variations on the type signatures of the extension methods avoid this type inference error, but these variants don't have the semantics I'm after.

+1  A: 

You can use Reflector and view your code with optimizations turned off.

Andrew Hare
No, as I said the code for the query expression doesn't compile, so there's nothing for me to examine. I'm looking for some tool/technique to translate the source code of a query expression to the source code using the equivalent extension methods so I can see why the query expression won't compile.
naasking
Can you post the code? Perhaps if I look at it I can help you figure out what is going on.
Andrew Hare
naasking
+4  A: 

Your query comprehension code is:

from f1 in e1
from f2 in e2
from f3 in e3
select f3

Your method call code is:

e1
.SelectMany(f1 => e2)
.SelectMany(f2 => e3), (f2, f3) => f3))

The query translation proceeds as follows. First we deal with the first two from clauses:

from f1 in e1
from f2 in e2
from f3 in e3
select f3;

This is translated into

from x in ( e1 ) . SelectMany( f1 => e2 , ( f1 , f2 ) => new { f1 , f2 } )
from f3 in e3
select f3;

Where "x" is a transparent identifier. Since none of e1, e2 or e3 consume any range variable, the fact that this is a transparent identifier is irrelevant; no further rewriting needs to be done to handle the transparent identifier semantics.

That result is then transformed into

( ( e1 ) . SelectMany( f1 => e2 , ( f1 , f2 ) => new { f1 , f2 } ) ) 
.SelectMany( x => e3 , ( x , f3 ) => f3 )

We can eliminate some of those parentheses:

e1 
.SelectMany( f1 => e2 , ( f1 , f2 ) => new { f1 , f2 } ) ) 
.SelectMany( x => e3 , ( x , f3 ) => f3 )

Clearly this is rather different from the syntactic transformation you've done manually, which, recall, was

e1
.SelectMany(f1 => e2)
.SelectMany(f2 => e3), (f2, f3) => f3))

If you substitute in your e1, e2, e3 into the actual syntactic transformation above, does the resulting expression pass type inference?

If it does not, then the question is "why not?" Either there's something wrong with your code, or something wrong with the type inferrer. If there's something wrong with the type inferrer, let me know.

If it does, then the question is "what's wrong with the syntactic transformation pass"? If there's something wrong with the syntactic transformation pass, again, let me know.

Thanks!

Eric Lippert
By the way, f1 and f2 are not in scope because the anonymous type is now encapsulated in a future.
naasking
Using your translation with a minor adjustment to the extension methods, I can get the query to compile properly when returning f3, but f1 and f2 are no longer in scope. The problem is that I'm operating on lifted future types, and I only want to extract the future's value from client code. So normally SelectMany looks like: Future<R> SelectMany<T, U, R>(this Future<T> fut, Func<T, Future<U>> sel, Func<T, U, R> res) But I need it to look like: Future<R> SelectMany<T, U, R>(this Future<T> fut, Func<Future<T>, Future<U>> sel, Func<Future<T>, Future<U>, Future<R>> res) Thoughts?
naasking
Actually, the following would suffice: Future<R> SelectMany<T, U, R>(this Future<T> fut, Func<Future<T>, Future<U>> sel, Func<Future<T>, Future<U>, R> res)This doesn't work either.
naasking
A: 

Eric's overview led me to understand how these queries are processed. The problem was that I was trying to constrain the types being operated on in a way that the query translation didn't like.

from x in Foo.Bar()
...

Foo.Bar() was supposed to return a Future and x was also supposed to be of type Future, but this doesn't work with query translation. I addressed this by adding another layer of indirection, basically wrapping futures in say, an Async<T> type, which could only be instantiated with futures, ie.

public sealed class Async<T> { internal T value; }
public static class Async
{
   public static Async<Future<T>> Begin<T>(Future<T> future) { ... }
}

Then I can write query computations on Async values, so the expression becomes something like:

from x in Async.Begin(Foo.Bar())
...

where x is now of type future and I can force or defer futures and promises arbitrarily.

Thanks for the suggestions everyone. A query expression translator built in to Visual Studio would be nice though, in case anyone at MS is reading this. ;-)

naasking