forward range properties with alias this - how?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 29 05:25:12 PDT 2015


On Tuesday, 28 July 2015 at 21:25:23 UTC, Ivan Kazmenko wrote:
> Hello,
>
> I wrap an array into a struct.  Then I use alias this to expose 
> the array functionality.  Sadly, range properties of the array 
> are not forwarded, and so I can't use the struct as an array 
> with functions from std.algorithm and std.range.
>
> -----
> import std.range, std.stdio;
> struct S {
> 	int[] contents;
> 	alias contents this;
> }
> void main() {
> 	S s;
> 	writeln(hasSlicing!(S)); // false
> }
> -----
>
> I would like to be able to do that, however.
>
> 1. Why exactly hasSlicing (et al.) does not check the alias 
> this-ed array when it checks the struct?
>
> 2. What should I do?
>
> The solution I can think of is to insert the 3-6 range 
> functions which forward the functionality to the underlying 
> array, perhaps as a mixin.
>
> Ivan Kazmenko.

`hasSlicing` explicitly checks whether the result of the slice 
operator has the same type as the original:

https://github.com/D-Programming-Language/phobos/blob/master/std/range/primitives.d#L1499-L1500

If you remove the `static assert()` and change the next line to 
use `auto`, and do the same in the other two places in this 
templates, it will work.

I don't know whether this is intentional. I'd say we should allow 
a sliceable range to have slices of a different type.

EDIT:
The documentation even says that it's intentional, but gives no 
justification.


More information about the Digitalmars-d-learn mailing list