www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - opDispatch returning this not working in a hierarchy

reply Daniel L. Alves <daniel_lopes_alves hotmail.com> writes:
Hi,
I don't know if this is really a bug or some gotcha of the language that I
don't get. I'm sorry if it happens to be the later.

When I run this simple program

class DispatchBase
{
    auto opDispatch( string m, Args... )( Args  args )
    {
        writefln( "Tried to call %s", m );
        return this;
    }
}

class DispatchDerived : DispatchBase
{
    void printValue( T )( T value )
    {
        writefln( "Value is %s", value );
    }
}

void main()
{
    DispatchBase base = new DispatchDerived();
    base.items.printValue( true );
}

I receive this output

Tried to call items
Tried to call printValue

But what I was expecting is

Tried to call items
Value is true

After all, opDispatch returns 'this'. The interesting thing is that when I
move opDispatch up to DispatchDerived, everything works fine. It's like 'this'
inside opDispatch didn't recognize its real type when in a base class.

Hope you can help me.
Daniel
Feb 01 2012
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 2/1/2012 8:00 PM, Daniel L. Alves wrote:
 I don't know if this is really a bug or some gotcha of the language that I
 don't get. I'm sorry if it happens to be the later.
This n.g. is mainly for bugzilla notifications. Questions like this should go into the digitalmars.D.learn n.g. But to answer your question, printValue is a template, and templates are not virtual. Also, DispatchBase.opDispatch knows nothing about DispatchDerived.
Feb 01 2012
prev sibling parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
There are some issues with mixing opDispatch and property syntax.  Try:
base.items().printValue( true );

This list is not for posting bug reports, it's just for bugzilla issues. 
Please post this kind of thing to d.learn or open a bug report at 
http://d.puremagic.com/issues/

"Daniel L. Alves" <daniel_lopes_alves hotmail.com> wrote in message 
news:jgd1oj$2r04$1 digitalmars.com...
 Hi,
 I don't know if this is really a bug or some gotcha of the language that I
 don't get. I'm sorry if it happens to be the later.

 When I run this simple program

 class DispatchBase
 {
    auto opDispatch( string m, Args... )( Args  args )
    {
        writefln( "Tried to call %s", m );
        return this;
    }
 }

 class DispatchDerived : DispatchBase
 {
    void printValue( T )( T value )
    {
        writefln( "Value is %s", value );
    }
 }

 void main()
 {
    DispatchBase base = new DispatchDerived();
    base.items.printValue( true );
 }

 I receive this output

 Tried to call items
 Tried to call printValue

 But what I was expecting is

 Tried to call items
 Value is true

 After all, opDispatch returns 'this'. The interesting thing is that when I
 move opDispatch up to DispatchDerived, everything works fine. It's like 
 'this'
 inside opDispatch didn't recognize its real type when in a base class.

 Hope you can help me.
 Daniel 
Feb 01 2012