www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - alias this and associative arrays

reply Shachar Shemesh <shachar weka.io> writes:
import std.exception;

struct AA(Key, Val) {
     Val[Key] aa;
     alias aa this;

     void opIndexAssign(inout Val value, Key key) pure {
         aa[key] = value;
     }
}

void main() {
     AA!(string, int) a;
      //compile error -- no property 'remove' for type 
'CheckedAA!(string, int)'
     a.remove("aaa");
}

What's wrong here?
Apr 22 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/22/18 7:06 AM, Shachar Shemesh wrote:
 import std.exception;
 
 struct AA(Key, Val) {
      Val[Key] aa;
      alias aa this;
 
      void opIndexAssign(inout Val value, Key key) pure {
          aa[key] = value;
      }
 }
 
 void main() {
      AA!(string, int) a;
       //compile error -- no property 'remove' for type 
 'CheckedAA!(string, int)'
      a.remove("aaa");
 }
 
 What's wrong here?
It's a bug. The remove function is not actually defined by the library, it's a compiler special that remaps to an extern(C) function _aaDelX. As far as I can tell, it's the only one that doesn't work. All the others seem to work (e.g. a.keys). What I would guess is happening is the compiler is trying to call remove on the AA but has skipped the part that translates to _aaDelX. It would be good to hoist this out of the compiler and make it a UFCS call, which would also fix the problem. -Steve
Apr 22 2018