Re: opIndexUnary post in-/decrement how to ?

2021-07-16 Thread wjoe via Digitalmars-d-learn
On Thursday, 15 July 2021 at 15:39:59 UTC, Tejas wrote: On Thursday, 15 July 2021 at 13:28:19 UTC, wjoe wrote: On Thursday, 15 July 2021 at 12:09:20 UTC, Tejas wrote: [...] The only way, for me, to explain the error message ```opIndex isn't an lvalue and can't be modified.``` for

Re: opIndexUnary post in-/decrement how to ?

2021-07-15 Thread Tejas via Digitalmars-d-learn
On Thursday, 15 July 2021 at 13:28:19 UTC, wjoe wrote: On Thursday, 15 July 2021 at 12:09:20 UTC, Tejas wrote: [...] The only way, for me, to explain the error message ```opIndex isn't an lvalue and can't be modified.``` for ```i[1]++``` is that the compiler rewrites to ```D (auto e =

Re: opIndexUnary post in-/decrement how to ?

2021-07-15 Thread wjoe via Digitalmars-d-learn
On Thursday, 15 July 2021 at 12:09:20 UTC, Tejas wrote: [...] Oh yes, that is what happens. I was trying to be a little concise. You are correct, this is what the code will look in the gory details (I believe) : ```d auto x = (auto e = i.opIndex(1), i.opIndexUnary("++")(1)/*this may or may

Re: opIndexUnary post in-/decrement how to ?

2021-07-15 Thread Tejas via Digitalmars-d-learn
On Thursday, 15 July 2021 at 11:02:17 UTC, wjoe wrote: On Thursday, 15 July 2021 at 04:07:49 UTC, Tejas wrote: Your code ```d auto x = i[1]++; ``` Expands to: ```d auto x = (auto e = i[1]/*notice opIndex*/, ++i[1]/* notice opIndexUnary*/, return e;); ``` This doesn't happen with pre

Re: opIndexUnary post in-/decrement how to ?

2021-07-15 Thread wjoe via Digitalmars-d-learn
On Thursday, 15 July 2021 at 04:07:49 UTC, Tejas wrote: Your code ```d auto x = i[1]++; ``` Expands to: ```d auto x = (auto e = i[1]/*notice opIndex*/, ++i[1]/* notice opIndexUnary*/, return e;); ``` This doesn't happen with pre increment. No compiler shenanigans. Interesting to see it

Re: opIndexUnary post in-/decrement how to ?

2021-07-15 Thread wjoe via Digitalmars-d-learn
On Thursday, 15 July 2021 at 04:01:15 UTC, Tejas wrote: I'm so sorry all this was basically useless for you. I can't spend more time on this, so as a last resort I leave you this: https://dlang.org/phobos/std_bitmanip.html This is the official bit manipulation standard library, maybe it

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 20:55:32 UTC, wjoe wrote: In my particular case the compiler can rule out ```opIndex``` so why does it abort instead of trying ```opIndexUnary``` ? Or was it trying and it didn't work ? If that's the case I'd like to know the reason why it discarded

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 20:55:32 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 16:13:35 UTC, Tejas wrote: [...] Congratulations:) Unfortunately I haven't got anything I could return by ref so I can't take advantage of a low hanging fruit. In my book overloading operators is no fun -

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread wjoe via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 16:13:35 UTC, Tejas wrote: On Wednesday, 14 July 2021 at 15:08:56 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 14:50:01 UTC, Mike Parker wrote: On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote: [...] It's how the contract of post-inc/dec

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Ali Çehreli via Digitalmars-d-learn
On 7/14/21 11:27 AM, Tejas wrote: > the compiler error should have been a dead giveaway > that opIndex was returning a _constant_ value I know you mean "rvalue" but if I may be unnecessarily pedantic, an rvalue can indeed be mutated: struct A { int i; void mutate() { i = 42;

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 17:42:03 UTC, Ali Çehreli wrote: On 7/14/21 9:13 AM, Tejas wrote: > ref/*notice this ref*/ int opIndex(int index)return/*NOTICE THE > RETURN*/ { Indeed... I cover that 'ref' here:

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Ali Çehreli via Digitalmars-d-learn
On 7/14/21 9:13 AM, Tejas wrote: > ref/*notice this ref*/ int opIndex(int index)return/*NOTICE THE > RETURN*/ { Indeed... I cover that 'ref' here: http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.return%20type,%20operator Two quotes from that section: 1) "it

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 15:08:56 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 14:50:01 UTC, Mike Parker wrote: On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote: [...] It's how the contract of post-inc/dec work---pre-inc/dec return the modified value, post-inc/dec return the

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 15:23:05 UTC, vit wrote: On Wednesday, 14 July 2021 at 15:19:03 UTC, vit wrote: ```d import std.stdio; struct abc{ int[100] a; struct Proxy{ abc* ptr; const int index; int opUnary(string op : "++")(){ return

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 15:23:05 UTC, vit wrote: On Wednesday, 14 July 2021 at 15:19:03 UTC, vit wrote: ```d import std.stdio; struct abc{ int[100] a; struct Proxy{ abc* ptr; const int index; int opUnary(string op : "++")(){ return

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread vit via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 15:19:03 UTC, vit wrote: ```d import std.stdio; struct abc{ int[100] a; struct Proxy{ abc* ptr; const int index; int opUnary(string op : "++")(){ return ++ptr.a[index]; //add missing ++ } }

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
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

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread vit via Digitalmars-d-learn
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{

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread wjoe via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 14:50:01 UTC, Mike Parker wrote: On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote: [...] It's how the contract of post-inc/dec work---pre-inc/dec return the modified value, post-inc/dec return the original value. [...] That makes a lot of sense now,

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread wjoe via Digitalmars-d-learn
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

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 11:31:36 UTC, Tejas wrote: ``` {auto a = i[1] , ++i[1] , a} //note the , not the ;``` Sorry I can't provide something even more concrete. Yes I saw that, and I suppose it would work just fine if it were

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread wjoe via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 12:49:58 UTC, Tejas wrote: I think it's a bug, because the following works: ```d import std.stdio; struct abc{ int[100] a; int opIndex(int index){ return a[index]; } int opIndexUnary(string s)(int index) if(s == "++"){

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread vit via Digitalmars-d-learn
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

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 13:09:56 UTC, vit wrote: On Wednesday, 14 July 2021 at 12:49:58 UTC, Tejas wrote: On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 11:31:36 UTC, Tejas wrote: ``` {auto a = i[1] , ++i[1] , a} //note the , not the ;``` Sorry I

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread vit via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 12:49:58 UTC, Tejas wrote: On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 11:31:36 UTC, Tejas wrote: ``` {auto a = i[1] , ++i[1] , a} //note the , not the ;``` Sorry I can't provide something even more concrete. Yes I saw

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 11:31:36 UTC, Tejas wrote: On Wednesday, 14 July 2021 at 10:07:38 UTC, wjoe wrote: I'm want to do something like this ```D part_int_t!(1,2,3) i; auto x = -i[0]; --i[1]; // 1 i[1]++; // 2 ``` I think the operator I need to overload would be opIndexUnary which I

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 11:31:36 UTC, Tejas wrote: ``` {auto a = i[1] , ++i[1] , a} //note the , not the ;``` Sorry I can't provide something even more concrete. Yes I saw that, and I suppose it would work just fine if it were

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread wjoe via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 11:31:36 UTC, Tejas wrote: ``` {auto a = i[1] , ++i[1] , a} //note the , not the ;``` Sorry I can't provide something even more concrete. Yes I saw that, and I suppose it would work just fine if it were rewritten to just ```++i[1]```. What I'm struggling to

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread Tejas via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 10:07:38 UTC, wjoe wrote: I'm want to do something like this ```D part_int_t!(1,2,3) i; auto x = -i[0]; --i[1]; // 1 i[1]++; // 2 ``` I think the operator I need to overload would be opIndexUnary which I did. (1) compiles. (2) doesn't - the compiler complains

opIndexUnary post in-/decrement how to ?

2021-07-14 Thread wjoe via Digitalmars-d-learn
I'm want to do something like this ```D part_int_t!(1,2,3) i; auto x = -i[0]; --i[1]; // 1 i[1]++; // 2 ``` I think the operator I need to overload would be opIndexUnary which I did. (1) compiles. (2) doesn't - the compiler complains that i.opIndex isn't an lvalue and can't be modified. The