Cannot implicitly convert derived type

Ali Çehreli acehreli at yahoo.com
Sat Feb 22 19:34:20 PST 2014


On 02/22/2014 12:17 PM, Frustrated wrote:

 > Again, the whole point of why it is illegal because you can pass
 > a RogueButton... BUT I DON'T INTEND TO PASS THEM!

Thinking that way, many rules of a statically type-checked language like 
D would be unnecessary. ;)

 > WindowsGui only uses WindowsButton which is a iButton type.

WindowsGui is totally free to use any type it wants. However, it cannot 
both claim to implement an interface without actually obeying its 
requirements.

 > Why can't I relax the condition to use the base type?

The users of iGui don't even know what a WindowsButton is:

interface iGui
{
     @property iButton button(ref iButton button);
}

Imagine the following that I write:

void foo(WindowsGui gui, iButton b)
{
     gui.button(b);
}

Is the call legal or not? How would I know and why should I care? I have 
a WindowsGui, which happens to be an iGui and I have an iButton. 
According to the contract of the interface I should be able to call it 
without fear of type problems.

Even, I can create my own iButton and pass it to that WindowsGui:

     gui.button(new MyButton());

I should be able to do all of that just because WindowsGui promises to 
be an iGui.

Getting back to what I said above: WindowsGui is totally free to use any 
type it wants. That's why both Steven and I made it take its button as a 
constructor parameter. Then, you also mentioned a setter. That's fine 
too. As long as WindowsGui allows me to give it an iButton when I call 
button(), it is all fine.

Ali



More information about the Digitalmars-d-learn mailing list