Can you simplify nested Indexed types?

Tejas notrealemail at gmail.com
Wed Dec 28 02:59:34 UTC 2022


On Tuesday, 27 December 2022 at 15:09:11 UTC, Sergei Nosov wrote:
> Consider, I have the following code:
>
> ```
>     auto a = [3, 6, 2, 1, 5, 4, 0];
>
>     auto indicies = iota(3);
>     auto ai = indexed(a, indicies);
>     ai = indexed(ai, iota(2));
>
>     writeln(ai);
> ```
>
> Basically, my idea is to apply `indexed` to an array several 
> times and have all the intermediaries saved in the same 
> variable. The provided code doesn't compile with an error:
>
> ```
> Error: cannot implicitly convert expression `indexed(ai, 
> iota(2))` of type `Indexed!(Indexed!(int[], Result), Result)` 
> to `Indexed!(int[], Result)`
> ```
>
> I wonder, if there's a way to "collapse" or "simplify" the 
> `Indexed!(Indexed!(int[], Result), Result)` type to just 
> `Indexed!(int[], Result)` ?

Well, pretty sure this isn't what you meant by "same variable" 
but since it _technically_ does what you want, I decided to share 
it: Basically I'm abusing `array` and this thing might be pretty 
memory heavy...

```d
import std;

void main()
{

     auto a = [3, 6, 2, 1, 5, 4, 0];

     auto indices = iota(3);
     auto ai = indexed(a, indices).array;
     ai = indexed(ai, iota(2)).array;

     writeln(ai); // [3, 6]
}
```


More information about the Digitalmars-d-learn mailing list