Should be easy

Saaa empty at needmail.com
Fri Jun 12 17:01:36 PDT 2009


> Saaa wrote:
>> I can't figure out how to create the IndexArray function,
>> it should work on arrays of any depth
>
> BaseType!(TArray) index(TArray : TArray[])(TArray array, int[] indices...)
> {
> return index(array[indices[0]], indices[1..$]);
> }
>
> TElement index(TElement)(TElement element, int[] ignored...)
> {
> return element;
> }
>
> This uses template specialization to handle arrays differently than 
> non-arrays (that's what the "TArray : TArray[]" business means). It uses 
> the BaseType template worked out in one of your previous threads. It's 
> close enough to being the simplest thing that could work that I wouldn't 
> bother looking for anything simpler.

I really like templates :D
I actually tried something like this, but I couldn't get it to compile.
My try misses quit a few things I see

void IndexArray(T : T[], U)(T array, U index)
{
  IndexArray( array[index[0]][], index[1..$]);
}

void IndexArray(T : int, U)(T array, U index)
{
  array[index[0]] = -7;//test
}

I did get it to compile at one time, but didn't know how to use it,
like your code..
index( array, index2);  //compiles and all, but how do I set the value?
index( array, index2) = -1; // doesn't work
Also, why the ... ?




More information about the Digitalmars-d-learn mailing list