« 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

Comment from: Peter Ritchie [Visitor]
Peter RitchieYep, no love for that in 4 either. Maybe vNext (fingers crossed)...
03/09/10 @ 19:43
Comment from: Igor Dontsov [Visitor] Email
Igor DontsovCheck 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 [Member] Email
Oliver SturmHey 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.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)
Please complete the song title below. Hint: enter 's', 'a', 't', 'i', 's', 'f', 'a', 'c', 't', 'i', 'o', 'n'
antispam test

Enter your email address:

Search

Oliver
MVP logo
May 2012
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