static opSlice is not possible

Basile B. b2.temp at gmx.com
Fri Feb 16 13:35:03 UTC 2018


On Friday, 16 February 2018 at 13:23:09 UTC, Basile B. wrote:
> On Thursday, 15 February 2018 at 22:49:56 UTC, Alex wrote:
>> Hi all,
>> a short question about an old bug:
>> https://issues.dlang.org/show_bug.cgi?id=11877
>>
>> Are there reasons, which speaks against this feature?
>>
>> And maybe another one, more general:
>> Is there any place, where it is documented, which operators 
>> can work in static mode and which cannot?
>
> There's a hack to make static slicing working actually.
> I've used it once to make a kind of set of character. Reduced 
> example:
>
>
> ```
> struct Foo
> {
>     static auto opSlice(int index)(size_t lo, size_t hi)
>     if (index == 0)
>     {
>         return 42;
>     }
>     static auto opIndex(A...)(A a)
>     {
>         return 42;
>     }
> }
>
> static assert(Foo[0..1337] == 42);
> ```
>
> I don't know if it is possible by error, maybe they forgot to 
> disable this form of slicing.

Technically iy's a multi dimensional slicing but there's a 
constraint on the number of dimension allowed so that it looks 
exactly like a normal opSlice.

By the way, i reduced too much. This shows more how it works:

struct Foo
{
     static auto opSlice(int index)(size_t lo, size_t hi)
     if (index == 0)
     {
         return 41;
     }
     static auto opIndex(A...)(A a)
     {
         return opSlice!0(0,0) + 1;
     }
}

static assert(Foo[0..1337] == 42);


More information about the Digitalmars-d-learn mailing list