opIndexUnary post in-/decrement how to ?

wjoe invalid at example.com
Wed Jul 14 15:01:45 UTC 2021


On Wednesday, 14 July 2021 at 14:39:03 UTC, vit wrote:
> On Wednesday, 14 July 2021 at 13:16:49 UTC, Tejas wrote:
>> On Wednesday, 14 July 2021 at 13:09:56 UTC, vit wrote:
>>> On Wednesday, 14 July 2021 at 12:49:58 UTC, Tejas wrote:
>>>> [...]
>>>
>>> From doc: https://dlang.org/spec/operatoroverloading.html
>>> Postincrement e++ and Postdecrement e-- Operators
>>> These are not directly overloadable, but instead are 
>>> rewritten in terms of the ++e and --e prefix operators:
>>>
>>> Postfix Operator Rewrites
>>> op	rewrite
>>> e--	(auto t = e, --e, t)
>>> e++	(auto t = e, ++e, t)
>>>
>>>
>>> Rewriting part doesn't work with operator overloading.
>>
>> If the rewriting part doesn't work with overloading then why 
>> aren't we allowed to explicitly overload 
>> ```post-increment/decrement``` operators?
>>
>> How else will the OP solve their problem?
>>
>> It must rewrite,  otherwise it's impossible (I think).
>
>
> 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.



More information about the Digitalmars-d-learn mailing list