« Creating a Lazy Sequence of Directory Descendants in F#Google's precision is getting worse »

The yield statement cannot be used inside an anonymous method or lambda expression

09/03/10

Permalink 07:26:39 pm
Categories: General, Programming, .NET

The yield statement cannot be used inside an anonymous method or lambda expression

I thought I was being pretty cool with this code:

IEnumerator<t> System.Collections.Generic.IEnumerable<t>.GetEnumerator( ) {
  Action<unbalancedbinarytree <T>> yielder = null;
  yielder = t => {
    yielder(t.Left);
    if (!t.IsEmpty)
      yield return t.Value;
    yielder(t.Right);
  };
  yielder(this);
}

But as it turns out (and I think I knew before, but had forgotten), C# doesn’t accept this. Not in 3.0 at least, and I think not in 4.0 either. Need to test. Meanwhile – why? Hm…

3 comments

Yep, no love for that in 4 either. Maybe vNext (fingers crossed)...
03/09/10 @ 19:43
Comment from: Igor Dontsov Email
Check this post by Eric Lippert: "Iterator Blocks Part Seven: Why no anonymous iterators?" http://blogs.msdn.com/ericlippert/archive/2009/08/24/iterator-blocks-part-seven-why-no-anonymous-iterators.aspx
03/09/10 @ 22:16
Comment from: Oliver Sturm Email
Hey Igor,

Thanks for the link. Yeah... sounds a bit like "we don't really think it's quite that useful and we're scared of getting it wrong" :-) Legitimate reasons I guess, but still a pity.
03/09/10 @ 22:30

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Please complete the song title below. Hint: enter 'h', 'e', 'a', 'v', 'e', 'n'
antispam test

Enter your email address:

Search

Oliver
MVP logo
July 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31