Question

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.

Was it helpful?

Solution

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!

OTHER TIPS

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

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. ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top