Someone's just discovered "yield"



  • A delightful pattern that recurs throughout a library I've just opened up...

    public IEnumerable<Foo> GetFoos()

    {

         foreach (Foo f in PrivateGetFoos())

             yield return f;



  • So does that return a IEnumerable of IEnumerables? Or... I'm confused about what this even does, although I'm sure it's not what he intended.

    Edit: oh I get it. He obviously already has an IEnumerable<Foo> (returned by PrivateGetFoos()), so the only thing he needs to do is return it.

    Instead, he loops through it and tells C# to make another IEnumerable<Foo> from each element in the one he already had, then returns that. Smart.

    This function, written by a non-idiot, would just be: return PrivateGetFoos();


  • Discourse touched me in a no-no place

    @Julia said:

    A delightful pattern that recurs throughout a library I've just opened up...
    I'm surprised they don't collect everything from the private method into a list, and then yield/iterate over that…



  • Is the IEnumerable modified by the calling code? If so, this would allow modification of that IEnumerable without altering the original one. Though this is probably a WTF even if that's the case.



  • @mott555 said:

    Is the IEnumerable modified by the calling code? If so, this would allow modification of that IEnumerable without altering the original one. Though this is probably a WTF even if that's the case.

    You cannot modify an IEnumerable.



  • @Ragnax said:

    @mott555 said:
    Is the IEnumerable modified by the calling code? If so, this would allow modification of that IEnumerable without altering the original one. Though this is probably a WTF even if that's the case.

    You cannot modify an IEnumerable.

     

    ...unless you cast it to the original type.  This could be to prevent someone with too much knowledge of internals screwing things up, by creating a new sequence instead of returning the original one.

    Or it could be a total WTF.



  •  @Mason Wheeler said:

    ...unless you cast it to the original type.

    Of course, that would be a WTF of it's own.

     



  • Someone mind explaining this for people who can't even tell what language this is?



  • @henke37 said:

    Someone mind explaining this for people who can't even tell what language this is?

    My posts are invisible!



  • It's C#. Apparently C# has a special language construct for iterators that allows you to break off and resume a function call at the 'yield' point. I'm no C# expert, but I'm pretty sure the guy is doing it wrong.

    http://msdn.microsoft.com/en-us/library/vstudio/9k7k7cf0.aspx



  • @henke37 said:

    Someone mind explaining this for people who can't even tell what language this is?
     

    It's C#.  The .NET framework has a concept of enumerable sequences, represented by the IEnumerable interface, which is a very abstract, very generic type that supports two operations: "get the current item in the sequence" and "attempt to advance to the next item in the sequence."

    Generator methods in C# use the "yield return" keyword to create sequences one item at a time.  There's a lot of compiler magic involved in making it happen, it's actually pretty fascinating if you're into that sort of thing.  Eric Lippert, one of the architects of the system, did a series of blog posts on it that explain the gory details.  (Look at the ones titled "Iterator Blocks, Part ###".)

    What this code is doing is taking an existing sequence, iterating over each element in the sequence, and yielding it (completely unchanged) as an element of a new sequence.  The point being made by the OP is that this seems like a silly and needless alternative to simply returning the original sequence.



  • @Mason Wheeler said:

    The point being made by the OP is that this seems like a silly and needless alternative to simply returning the original sequence.
    That would be a perfectly fair point if you were the kind of ableist who believes that it is right and proper for programming languages to support abstractions that are not instantly obvious to any user.



  • @flabdablet said:

    @Mason Wheeler said:
    The point being made by the OP is that this seems like a silly and needless alternative to simply returning the original sequence.
    That would be a perfectly fair point if you were the kind of ableist who believes that it is right and proper for programming languages to support abstractions that are not instantly obvious to any user.
     

    Blakey?



  • @flabdablet said:

    @Mason Wheeler said:
    The point being made by the OP is that this seems like a silly and needless alternative to simply returning the original sequence.
    That would be a perfectly fair point if you were the kind of ableist who believes that it is right and proper for programming languages to support abstractions that are not instantly obvious to any user.
     

    Is anything in programming "instantly obvious to any user" without training in the language?

    What this routine is is instantly obvious to anyone familiar with the concept.  I'm not even a C# developer, but I could immediately tell what it was doing because I've read a bunch of stuff that Eric Lippert and Jon Skeet wrote about enumerable sequences and LINQ.



  • @Mason Wheeler said:

    Is anything in programming "instantly obvious to any user" without training in the language?
    If it's not, that just shows that programming sucks. Computers are supposed to be smart. They should just be able to do what we want, without all this text based bullshit. Did Hypercard have enumerables and LINQ? Didn't think so.



  • @blakeyrat said:

    @henke37 said:
    Someone mind explaining this for people who can't even tell what language this is?

    My posts are invisible!

     

    You got hellbanned...



  • @topspin said:

    @blakeyrat said:

    @henke37 said:
    Someone mind explaining this for people who can't even tell what language this is?

    My posts are invisible!

     

    You got hellbanned...

     

    Psst! We don't tell people that they are hellbanned. That defeats the pourpose.

     



  • @Mcoder said:

    @topspin said:

    @blakeyrat said:

    @henke37 said:
    Someone mind explaining this for people who can't even tell what language this is?

    My posts are invisible!

     

    You got hellbanned...

     

    Psst! We don't tell people that they are hellbanned. That defeats the pourpose.

     


    But I can see blakey's posts! Does that mean I'm hellbanned too?


  • Winner of the 2016 Presidential Election

    Guys, I haven't heard from blakey or Ben L in a while. Did one ragequit and the other get grounded for feline misconduct?



  • @joe.edwards said:

    Guys, I haven't heard from blakey or Ben L in a while. Did one ragequit and the other get grounded for feline misconduct?

    Filed under: I'm not saying which happened to which.

    Since nobody can see this, my password is hunter2 and my bank account number is an integer.

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.