Can you simplify nested Indexed types?

Sergei Nosov sergei.nosov at gmail.com
Wed Dec 28 06:44:35 UTC 2022


On Tuesday, 27 December 2022 at 16:43:49 UTC, Ali Çehreli wrote:
> On 12/27/22 07:09, Sergei Nosov wrote:
> If what you are looking for is a way of defining a variable for 
> "any InputRange that produces a specific type (size_t in this 
> case)", then there is inputRangeObject, which uses OOP:
>
>   https://dlang.org/phobos/std_range_interfaces.html#InputRange
>
> I have an additional example here:
>
>
> http://ddili.org/ders/d.en/ranges_more.html#ix_ranges_more.inputRangeObject
>
> Ali

Thanks, everyone!

I guess, this answer is the closest to what I was looking for. 
Somehow, I missed the range interfaces (and was considering to 
use `Variant` or smth). It does seem to answer the original 
question, albeit with layer(s) of indirection.

```
   auto indicies = iota(3);
   RandomAccessFinite!int ai = indexed(a, 
indicies).inputRangeObject;
   ai = indexed(ai, iota(2)).inputRangeObject;
```

Still, my gut feel is that some compile-time solution is possible 
- will, probably, tinker with it for a little more.

> Why not use filter(), isn't it important to filter out what's 
> in range?

That does something different.

> 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...

Yeah, using arrays is another alternative, but as you mention, it 
uses more memory and makes index evaluation eager (vs lazy).




More information about the Digitalmars-d-learn mailing list