Code layout for range-intensive D code

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 10 18:01:45 PDT 2012


On Sunday, June 10, 2012 17:31:37 Jonathan M Davis wrote:
> On Monday, June 11, 2012 02:11:24 ixid wrote:
> > Thank you. May I ask though, is the argument against
> > automatically appending .array() when a single or chain of lazy
> > functions are used to set a variable or set of variables just
> > syntactic salt against accidentally doing it eagerly?

Ouch, I obviously need to do a better job of re-reading my posts.

> D doesn't do much of anything like that automatically, Aside from the fact
> tha foreach supports the ranges, _nothing_ in the compiler supports them.

"D doesn't do much of anything like that automatically_._ Aside from the fact 
_that_ foreach supports ranges, nothing in the compiler supports them."

> They're entirely a library artifact. So, the compiler isn't going to do
> _anything_ to them automatically. And besides, it's not necessarily the
> case that you want the result of a range-based function to be an array.
> What if I want to keep the result of map on the stack?
> 
> auto a = map!"to!string(a)"(arr);
> 
> I could pass it to multiple functions later without having to allocate
> anything. Forcing it to be an array would stop that. And it would be
> downright nasty for that to happen when dealing with containers, because a
> number of containers require that you pass them the _exact_ range type that
> you give them (e.g. remove does this).

"a number of containers require that you pass them the exact range type that 
_they_ give _you_..."

> Converting those ranges to arrays
> just because you assigned them to a variable would be a _big_ problem.
> 
> And as for infinite ranges again, if they automatically were converted to
> arrays when assigned to variables, then you couldn't have variables for them
> at all, because they _have_ to be eager.

"because they have to be _lazy_."

- Jonathan M Davis


More information about the Digitalmars-d mailing list