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?

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. 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). 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. Take a random number generator for 
instance. If all ranges were automatically converted to arrays when assigned 
to variables, then you couldn't do

auto generator = rndGen();

You'd be stuck in an infinite loop if you tried.

Having to use std.array.array to explicitly convert a range to an array really 
doesn't cost you much, and trying to do it automatically would be incredibly 
error-prone and costly. And really, the more that you use rang-based 
functions, the less that you need to convert ranges to arrays.

- Jonathan M Davis

Reply via email to