重载 opIndexAssign

发布于 2024-11-19 03:21:39 字数 2430 浏览 5 评论 0原文

我似乎在我的一个类中重载 opIndexAssign 时遇到了一些问题。

我有课; JSObject 的定义如下:

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 ); 
    }

}

Dobject 超类重载了 put() 和 get() 方法,我试图包装它们,以便可以将它们作为关联数组访问:

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

它适用于 String,String 方法,但是当我尝试使用 JSObject 作为值,但失败了。

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.

我对自己做错的事感到有点茫然。这就像编译器尝试将其转换为适合 opIndexAssign( String, String ) 而不是 opIndexAssign( String, JSObject ) 方法。

我是否错误地定义了 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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2024-11-26 03:21:39

问题是 opIndexAssigne 首先需要值,然后是键(或索引)

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

所以你需要定义它,因为

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 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文