March 12, 2006
How about this?

foreach( ...; obj[m .. n] )

means

int obj.opSliceApply(m, n, int delegate(...) dg)

It is removed the necessity of making tmpObj(like struct) for opApply;

--
knjhara
March 12, 2006
knjhara wrote:

> How about this?
> 
> foreach( ...; obj[m .. n] )
> 
> means
> 
> int obj.opSliceApply(m, n, int delegate(...) dg)
> 
> It is removed the necessity of making tmpObj(like struct) for opApply;
> 
> --
> knjhara

How would this work with the current syntax?  Currently, obj[m .. n] will return a slice, then run .opApply(delegate...) on the object that the slice returns.  Your above syntax is inconsistent, as it means the compiler would ignore a slice operator in this case.

I'd imagine that you'd like this for efficiency reasons, so you don't have to create a new object (or worse, copy an array or something) in order to iterate through just a few of them.

If obj is a class of your own design, just make a method .iterSlice(n,m) which returns a struct which can efficiently iterate your objects.  It's not as pretty, but it is consistent.

If you're more concerned about D's built-in arrays- don't be.  If you slice an array, it doesn't immediately copy it.   Iterating through an array slice with the foreach shouldn't require any memory allocations.

...Or am I way off here?

~John Demme