I'm curious how the F# Seq computational expression implements the IEnumerable data structure.
Searching Sharp.Core/seq.fs on GitHub for the implementation of yield! ( YieldFrom(expr) ) isn't successful.
As the specification quoted by Daniel says, sequence expressions are not treated as ordinary computation expressions - rather than translating them to builder method calls (such as seq.For
, seq.Yield
etc.), the compiler actually generates code that is specific for sequences. This is mainly for performance reasons - the calls to computation builder would add too much overhead.
Here are two relevant pointers:
The GeneratedSequenceBase
type is the base class that is used by the compiler when compiling sequence expressions - there is also other library code around it that is used by the generated compiled code. See the F# library source code here.
The TcSequenceExpression
method in the type checker is where the type checking happens. You can see that the compiler is following a different path than for ordinary computation expressions. This is here in the compiler source code.
The other relevant parts are in ilxgen.fs
where the compiler actually generates the code after type checking - you can find the places by looking for GeneratedSequenceBase
in the source code (but I'm not an expert on this part)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With