www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - OpIndex/OpIndexAssign strange order of execution

reply SrMordred <patric.dexheimer gmail.com> writes:
struct Test{
      property int value(){
         writeln("property value : ", _value);
         return _value;
     }
     int _value;
     Test opIndex( string index )
     {
         writeln( "opIndex : index : ", index );
         return this;
     }

     Test opIndexAssign(int value, string index )
     {
         writeln( "opIndexAssign : value : ", value, " , index : 
", index );
         this._value = value;
         return this;
     }
}

Test t;
t["a"] = 100;
t["b"]["c"] = t["a"].value;

//OUTPUT:
opIndexAssign : index : a , value : 100
opIndex : index : b
opIndex : index : a
property value : 100
opIndexAssign : index : c , value : 100

//EXPECTED OUTPUT
opIndexAssign : index : a , value : 100
opIndex : index : a
property value : 100
opIndex : index : b
opIndexAssign : index : c , value : 100

Is this right?

I find unexpected this mix of operations on left and right side 
of an equal operator.
Sep 17
next sibling parent reply SrMordred <patric.dexheimer gmail.com> writes:
Should I report this as a bug?

I tried a C++ equivalent code and it execute in the expected 
order.
Sep 18
parent reply Moritz Maxeiner <moritz ucworks.org> writes:
On Sunday, 17 September 2017 at 18:52:39 UTC, SrMordred wrote:
 struct Test{ [...] }

 Test t;
As described in the spec [1]
 t["a"] = 100;
gets rewritten to --- t.opIndexAssign(100, "a"); --- , while
 t["b"]["c"] = t["a"].value;
gets rewritten to --- t.opIndex("b").opIndexAssign(t["a"].value, "c"); --- , which has to result in your observed output (left-to-right evaluation order):
 //OUTPUT:
 opIndexAssign : index : a , value : 100
 opIndex : index : b
 opIndex : index : a
 property value : 100
 opIndexAssign : index : c , value : 100

 //EXPECTED OUTPUT
 opIndexAssign : index : a , value : 100
 opIndex : index : a
 property value : 100
 opIndex : index : b
 opIndexAssign : index : c , value : 100

 Is this right?
AFAICT from the spec, yes. Your expected output does not match D's rewriting rules for operator overloading.
 I find unexpected this mix of operations on left and right side 
 of an equal operator.
Adding some more examples to the spec to show the results of the rewriting rules could be useful, but AFAICT it's unambiguous. On Monday, 18 September 2017 at 13:38:48 UTC, SrMordred wrote:
 Should I report this as a bug?
Not AFAICT.
 I tried a C++ equivalent code and it execute in the expected 
 order.
D does not (in general) match C++ semantics. [1] https://dlang.org/spec/operatoroverloading.html
Sep 18
parent reply Moritz Maxeiner <moritz ucworks.org> writes:
On Monday, 18 September 2017 at 15:11:34 UTC, Moritz Maxeiner 
wrote:
 gets rewritten to

 ---
 t.opIndex("b").opIndexAssign(t["a"].value, "c");
 ---
Sorry, forgot one level of rewriting: --- t.opIndex("b").opIndexAssign(t.opIndex("a").value, "c"); ---
Sep 18
parent SrMordred <patric.dexheimer gmail.com> writes:
On Monday, 18 September 2017 at 15:14:20 UTC, Moritz Maxeiner 
wrote:
 On Monday, 18 September 2017 at 15:11:34 UTC, Moritz Maxeiner 
 wrote:
 gets rewritten to

 ---
 t.opIndex("b").opIndexAssign(t["a"].value, "c");
 ---
Sorry, forgot one level of rewriting: --- t.opIndex("b").opIndexAssign(t.opIndex("a").value, "c"); ---
Nice, thanks for the explanation :) This will be a problem for me because in my use case the internal operations of the structs are stack-like manipulations. So in this case t["b"]["c"] = t["a"].value; instead of manipulating the internal stack with "a" then "b" and "c" I got this weird order of "b", "a", "c" messing with the internal stack logic. Well, back to work :)
Sep 18
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
Have you considered the multiple indexes?

   https://dlang.org/spec/operatoroverloading.html#index_assignment_operator

It may give you some power in execution order.

import std.stdio;

struct S {
     auto opIndex(string index) {
         writeln(index);
         return 42;
     }

     auto opIndexAssign(int value, string[] indexes...) {
         writeln(indexes);
     }
}

void main() {
     auto s = S();
     s["b", "c"] = s["a"];
}

Prints

a
["b", "c"]

Ali
Sep 18
parent SrMordred <patric.dexheimer gmail.com> writes:
 void main() {
     auto s = S();
     s["b", "c"] = s["a"];
 }

 Prints

 a
 ["b", "c"]

 Ali
I thought about this too, thanks!
Sep 18