Tech Off Thread

57 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

How about some syntax sugar for IEnumerable<>?

Back to Forum: Tech Off
  • User profile image
    littleguru

    I would find Foo* very confusing if it would be used for IEnumerable<Foo>. Foo* is a pointer and incrementing that pointer means that we move to the next element that sits nearby in memory. With IEnumerable that isn't true... there shouldn't be any Count in IEnumerable too. There's no guarantee that you can count the elements that are returned by the interface: it might be a live stream that is returned frame by frame.

  • User profile image
    Yggdrasil

    littleguru said:
    I would find Foo* very confusing if it would be used for IEnumerable<Foo>. Foo* is a pointer and incrementing that pointer means that we move to the next element that sits nearby in memory. With IEnumerable that isn't true... there shouldn't be any Count in IEnumerable too. There's no guarantee that you can count the elements that are returned by the interface: it might be a live stream that is returned frame by frame.
    If you're a C# developer, you quickly outgrow the "* as pointer" instinct. I haven't touched pointers in quite a while, and this is true even though I've written some C++ code lately - I've mostly used ^ managed pointers for C++/CLI. Smiley

    Regardless of whether you use * or any other mark that won't send C/C++ programmers into a tizzy, it has nothing to do with IEnumerable's Count method. While it's true that an IEnumerable is a lazy construct that isn't required to know at any given point in time how many elements it has in its possibly infinite sequence, there are still many (dare I say most?) scenarios where the Count is both knowable and relevant. I would rather have a Count() method that can (potentially) throw an exception or return a special value for an unknowable length, rather than disallow it entirely.

  • User profile image
    evildictait​or

    Yggdrasil said:
    littleguru said:
    *snip*
    If you're a C# developer, you quickly outgrow the "* as pointer" instinct. I haven't touched pointers in quite a while, and this is true even though I've written some C++ code lately - I've mostly used ^ managed pointers for C++/CLI. Smiley

    Regardless of whether you use * or any other mark that won't send C/C++ programmers into a tizzy, it has nothing to do with IEnumerable's Count method. While it's true that an IEnumerable is a lazy construct that isn't required to know at any given point in time how many elements it has in its possibly infinite sequence, there are still many (dare I say most?) scenarios where the Count is both knowable and relevant. I would rather have a Count() method that can (potentially) throw an exception or return a special value for an unknowable length, rather than disallow it entirely.
    No - C# has pointers, even if you don't use them, so C# couldn't introduce Type* as a shorthand for IEnumerable<Type> without a breaking change (and unsafe contexts are important for C# - they're used everywhere inside the .NET framework even if you can't see them).

    You might be able to "outgrow" the notion that Type* is a pointer, but that doesn't mean everyone can.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.