foreach over pointers OR foreach that mutates the iterator

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Jan 26 03:21:55 PST 2017


On Thursday, January 26, 2017 09:05:17 Las via Digitalmars-d wrote:
> On Wednesday, 25 January 2017 at 20:22:54 UTC, Jonathan M Davis
>
> wrote:
> > It only matters if you're trying to define the range primitives
> > for your range as free functions for some reason. Just put them
> > on the type itself and be done with it. The only reason that
> > wouldn't work would be if you weren't in control of the code
> > for the type in question, and I'm inclined to think that
> > turning a type that isn't a range into a range using free
> > functions isn't the best of ideas. You can always wrap the type
> > in another type though if you really want to turn it into a
> > range and can't just because you're dealing with a pointer.
> >
> > - Jonathan M Davis
>
> Strings use std.range.primitives.

Yes, because arrays do not have member functions. And as it is, there's been
discussion of moving those functions to object.d so that they're always
present rather than having to import std.range.primitives to get them (the
main blocker is that because of auto-decoding, all of that UTF mess would
have to then be available to object.d, which is in druntime, and std.utf is
in Phobos, but it may still happen). The fact that arrays get the range
primitives via UFCS and an import is required to use them is actually
problematic, much as it works (e.g. if you forget the import, things don't
work so well). With user-defined types, you have member functions and you
don't need UFCS.

- Jonathan M Davis



More information about the Digitalmars-d mailing list