F# under the covers XIII -- Sequence expressions
After more than a year since the previous instalment...
A C# method that returns an enumerator via yield return generates a synthetic class that looks like this:
where I omit unimportant detail based on the particular enumeration, but draw attention to how the other IEnumerator methods are implemented.
The F# result from a sequence expression is very different, with an explicit close and dispose:
and in GeneratedSequenceBase we have
The Close method renders the enumeration non-functional, though the closed enumerator does not throw, it simply yields no more. That the resulting sequence has an active Dispose behaviour, unlike the C# case, explains why in the Window example I needed to add the Controlled wrapper to block that, so that the Stream based enumeration written as a sequence expression would draw more than one tranche before stopping.
By contrast, you don't need the Controlled wrapper for windowing the BCL derived array-as-enumerator; presumably that has a no-op Dispose.
No comments :
Post a Comment