Reading through the F# 4.0 spec, I saw the following on page 79 of the PDF:
The auxiliary function
src(e)
denotesb.Source(e)
if the innermost ForEach is from the user code instead of generated by the translation, and a builderb
contains aSource
method. Otherwise,src(e)
denotese
.
This is in the context of the spec's detailed (VERY detailed) description of how computation expressions are parsed and turned into a series of method calls on the expression's builder object. Scott Wlaschin's Computation Expressions series, which I found to be invaluable in helping me understand the rest of the concepts behind computation expressions, doesn't mention a Source
method, nor does any other reference I've been able to find. (Google isn't much help with this one, as tons of people talk about source code and any references to Source
methods gets buried).
I also don't see Source
documented anywhere in the MSDN page on computation expressions. The QueryBuilder class uses Source
, so I have one example I can look at, but there's no explanation of why this would be useful in other circumstances.
Under what circumstances would you want to have a Source
method on a custom computation expression builder? What would be a scenario where the default ForEach
handling is inadequate to one's needs and a Source
method would be useful?
Source()
is being called with the contents of thein
clause (i.e.,for item in x
produces a call toSource(x)
). And I understand thatSource
should return something suitable to pass into theFor
. I don't quite see why, though.QueryBuilder.Source
wants either an IEnumerable or an IQueryable (which inherits from IEnumerable), both of which are suitable for aFor
function to iterate over. So why is theSource
call needed here? Why not just say "They gave me an IEnumerable, so I'll iterate over it"?For
I'm talking about isQueryBuilder.For
, which is called to handle thefor
expression inquery { **for** customer in db.Customers do select customer }
.