No aa.byKey.length?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 3 17:50:27 PDT 2016


On Sunday, April 03, 2016 23:46:10 John Colvin via Digitalmars-d-learn wrote:
> On Saturday, 2 April 2016 at 16:00:51 UTC, Jonathan M Davis wrote:
> > On Saturday, April 02, 2016 15:38:30 Ozan via
> >
> > Digitalmars-d-learn wrote:
> >> On Friday, 1 April 2016 at 20:50:32 UTC, Yuxuan Shui wrote:
> >> > Why?
> >> >
> >> > This is annoying when I need to feed it into a function that
> >> > requires hasLength.
> >>
> >> aa.keys.length
> >
> > That allocates an array. Doing that would be like doing
> > aa.byKeys().array().length. And associate arrays already have
> > length. You
> > can do
> >
> > auto len = aa.length;
> >
> > The problem is when you want to operate on a range, and the
> > function that you want to pass it to wants length on the range.
> > If byKeys returned a range with length, then that would work,
> > but since it doesn't, it doesn't. Having other ways to get the
> > length doesn't help.
>
> Maybe
>
> aa.byKey().takeExactly(aa.length)

Yeah, that's a clever workaround.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list