On Wednesday, 14 July 2021 at 15:01:45 UTC, wjoe wrote:
On Wednesday, 14 July 2021 at 14:39:03 UTC, vit wrote:
On Wednesday, 14 July 2021 at 13:16:49 UTC, Tejas wrote:
[...]


This work:

```d
import   std.stdio;

struct abc{
    int[100] a;

    ref int opIndex(int index)return{
        return a[index];
    }
}

void main (){
    abc s;
    s[0]++;
    ++s[0];
}

```

This does work for that example but I can't return by reference because what I'm operating on is a part of an int.
Some context:
```D
struct part_int_t(ARGS...)
{
   int _int;
   mixin(generate_parts!ARGS);
}

alias handle_t = part_int_t!("isAllocated", 1, "gen", 8, "index", 23);

handle_t handle;

static assert (is(handle.typeof_isAllocated): bool));
static assert (is(handle.typeof_gen): ubyte));
static assert (is(handle.typeof_index): uint));

handle[2] = true;
handle.gen = 1;
handle.index = 1234;

assert (handle.isAllocated);
assert (handle.gen = 1);
assert (handle[0] = 1234);

handle++;
assert (handle.index == 1235);

handle.gen++;
assert (handle.gen == 2);

handle.reset();
assert (!handle.isAllocated);
assert (handle.gen = 3);
assert (handle.index = 0);

```

generate_parts!ARGS produces bit masks, getters, setters, opIndex/OpAssign/Unary, etc. and it's impossible to return bits 0-26 of _int by ref.

Try something like this:

```d
import   std.stdio;

struct abc{
    int[100] a;

    struct Proxy{
        abc* ptr;
        const int index;

        int opUnary(string op : "++")(){
                return ptr.a[index];
        }
    }

    Proxy opIndex(int index)return{
        return Proxy(&this, index);
    }
}

void main (){
    abc s;
    s[0]++;
    ++s[0];
}
```

Reply via email to