Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overloading opIndexAssign

I seem to have some trouble overloading opIndexAssign in one of my classes.

I have a class; JSObject which is defined like this:

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 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 using the JSObject as the 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'm a bit at loss to what I'm doing wrong. It's like the compiler tries to cast it to fit with opIndexAssign( String, String ) instead of the opIndexAssign( String, JSObject ) method.

Did I define the opIndexAssign functions incorrectly?

Thanks in advance,

like image 843
Frederik Avatar asked Feb 24 '23 15:02

Frederik


1 Answers

the issue is that opIndexAssigne needs the value first and then the keys (or indices)

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

so you'll 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 so that you can define a vararg for the index

like image 137
ratchet freak Avatar answered Mar 05 '23 01:03

ratchet freak