Cannot implicitly convert derived type

Steven Schveighoffer schveiguy at yahoo.com
Sat Feb 22 17:37:07 PST 2014


On Sat, 22 Feb 2014 15:17:37 -0500, Frustrated <c1514843 at drdrb.com> wrote:

> It is legal exactly because I will always guarantee that the
> proper button will be used.

Static typing says it's not legal. D does not do dynamic type checking  
upon calling virtual functions.

> It is not logically legal as mentioned several times... no one
> needs to mention that. But it is legal within the context of the
> code. I'll never use a RogueButton so why force me to code for
> the chance I might?

You may not be the only one using WindowsGui. You can't guarantee other  
code won't do it. In any case, the compiler cannot possibly know your  
intentions.

> Basically, the point is, the compiler could enforce the above but
> make the code more readable.
>
> e.g.,
>
> I do this:
>
> @property WindowsButton button(WindowsButton b)
> {
>
> }
>
> The compiler turns this into
>
> @property WindowsButton button(iButton _b)
> {
>      if (is(_b : WindowsButton)) assert(0, "Rogue button used");
>      auto b = cast(WindowsButton)_b;
>
> }

This solution is not as efficient as the one I outlined. If you have a  
WindowsGui object, no need to accept iButton when you require  
WindowsButton.

> One allows me to program in a natural way while the other makes
> me jump through hoops which litters the code with a bunch of
> casts and checks which are only there in the odd event that I
> assign the wrong type(which, I'm not going to do on purpose).

Sorry, if you want a dynamically typed language, use one. D is not that.

> Again, the whole point of why it is illegal because you can pass
> a RogueButton... BUT I DON'T INTEND TO PASS THEM! If I could
> absolutely guarantee that I won't pass them then there should be
> no problem(no asserts). Since I can't guarantee it I have to
> litter my code with checks? The compiler could do this for me.

You can't guarantee it. That is the point. The compiler could do the  
checks for you, but D is not dynamically typed. The best you can do is  
encapsulate the type checking code as a mixin.

-Steve


More information about the Digitalmars-d-learn mailing list