Iterator Blocks Part Seven: Why no anonymous iterators?

This annotation to a comment in part five I think deserves to be promoted to a post of its own. Why do we disallow anonymous iterators? I would love to have anonymous iterator blocks.  I want to say something like: IEnumerable<int> twoints = ()=>{ yield return x; yield return x*10; }; foreach(int i in twoints)…

28

Iterator Blocks, Part Six: Why no unsafe code?

There are three good reasons to disallow unsafe blocks inside an iterator block. First, it is an incredibly unlikely scenario. The purpose of iterator blocks is to make it easy to write an iterator that walks over some abstract data type. This is highly likely to be fully managed code; it’s simply not a by-design scenario….

6

Iterator Blocks, Part Five: Push vs Pull

A while back I posted some commentary I did for the Scripting Summer Games where I noted that there is an isomorphism between “pull” sequences and “push” events. Normally you think of events as something that “calls you”, pushing the event arguments at you. And normally you think of a sequence as something that you…

13

Iterator blocks Part Four: Why no yield in catch?

Now that you know why we disallow yielding in a finally, it’s pretty straightforward to see why we also disallow yielding in a catch. First off, we still have the problem that it is illegal to “goto” into the middle of the handler of a try-protected region. The only way to enter a catch block…

14

Iterator Blocks, Part Three: Why no yield in finally?

There are three scenarios in which code could be executing in a finally in an iterator block. In none of them is it a good idea to yield a value from inside the finally, so this is illegal across the board. The three scenarios are (1) normal cleanup, (2) exception cleanup, and (3) iterator disposal….

10

Iterator Blocks, Part Two: Why no ref or out parameters?

A long and detailed discussion of how exactly we implement iterator blocks would take me quite a while, and would duplicate work that has been done well by others already. I encourage you to start with Raymond’s series, which is a pretty gentle introduction: part 1, part 2, part 3. If you want a more…

8

Iterator Blocks, Part One

There is a constant tension in language design between solving general problems and solving specific problems; finding the right spot on the general-to-specific spectrum can be quite tricky. The design of iterator blocks yields (ha ha) a germane example. At almost every step along the way, there are opportunities for making choices that determine whether…

8

Iterators at the Summer Games

Ed “Scripting Guy” Wilson was kind enough to ask me to be a guest commentator at this years Summer Scripting Games, which have just completed. I’ve been working on a series for this blog about some unusual cases in the design of the “iterator block” feature in C# 2.0; this bit from my commentary is…

14

Psychic Debugging, Part Two

A number of readers have the mysterious fifth sense which gives them the ability to deduce that the GetBars method from yesterday’s post contains a yield return and is therefore an iterator. Remember, as the standard states (in section 10.14.4): […] execution of the code in the iterator block occurs when the enumerator object’s MoveNext…

6

Psychic Debugging, Part One

Here is a compiler bug report I got the other day. The user is trying to write a unit test for a method which takes a Foo and returns a collection of Bars. The test is supposed to confirm that GetBars throws an exception if the argument is null. The test was failing with “got…

12