OpIndexAssign overload

It seems I have a problem with opIndexAssign overloading in one of my classes.

I have a class; JSObject, which is defined as follows:

alias char[] String; 

...

 class JSObject : Dobject { /***************************************************************** * Constructors ******************************************************************/ this( Dobject dobj ) { super( dobj ) ; } this() { super( null ) ; } this( AssociativeArray data ) { // initiate super( null ) ; // then populate foreach( k, v ; data ) { this[ k ] = v ; } } public void opIndexAssign( String key , String val ) { Value* v = new Value() ; v.putVstring( val ) ; this.Put(key, v , DontDelete); } public void opIndexAssign( String key , Dobject dobj ) { Value* v = new Value() ; v.putVobject( dobj ) ; this.Put(key, v , DontDelete); } public void opIndexAssign( String key , JSObject jso ) { Value* v = new Value() ; v.putVobject( jso ) ; this.Put(key, v , DontDelete); } public Value* opIndex( String key ) { return this.Get( key ); } } 

The Dobject superclass has overloaded put () and get () methods, and I'm trying to wrap them so that I can access them as associative arrays:

 77: JSObject jso = new JSObject() ; 78: jso[ "foo" ] = "bar" ; 79: 80: JSObject jsoParent = new JSObject() ; 81: jsoParent[ "child" ] = jso ; 

It works for the String, String method, but when I try to use JSObject as a value, it fails.

 test2.d => test2 + c:\dmd\dsss\bin\rebuild.exe -version=PhobosCompatibility -w -Idsss_imports\ -I. -S.\ -Ic:\dmd\dsss\include\d -Sc:\dmd\dsss\lib\ -Ic:\dmd\dsss\include\d -Sc:\dmd\dsss\lib -oqdsss_objs\D -debug -gc test2.d -oftest2 test2.d(81): Error: function dmdscripttest.JSObject.opIndexAssign (char[],char[]) does not match parameter types (JSObject,char[5u]) test2.d(81): Error: cannot implicitly convert expression (jso) of type dmdscripttest.JSObject to char[] test2.d(81): Error: cannot implicitly convert expression ("child") of type char[5u] to dmdscripttest.JSObject Error: Command failed, aborting. Command c:\dmd\dsss\bin\rebuild.exe returned with code 1, aborting. 

I lost a little what I am doing wrong. This is like a compiler trying to match it with opIndexAssign (String, String) instead of the opIndexAssign (String, JSObject) method.

Am I defining opIndexAssign functions?

Thanks in advance,

+4
source share
1 answer

the problem is that opIndexAssigne first requires a value, and then the keys (or indexes)

http://www.d-programming-language.org/operatoroverloading.html#Assignment

so you want to define it as

 public void opIndexAssign( String val , String key) { Value* v = new Value() ; v.putVstring( val ) ; this.Put(key, v , DontDelete); } public void opIndexAssign( Dobject dobj , String key) { Value* v = new Value() ; v.putVobject( dobj ) ; this.Put(key, v , DontDelete); } public void opIndexAssign( JSObject jso , String key) { Value* v = new Value() ; v.putVobject( jso ) ; this.Put(key, v , DontDelete); } 

the reason this is done is because you can define vararg for the index

+8
source

All Articles