Cannot implicitly convert derived type

Frustrated c1514843 at drdrb.com
Sun Feb 23 13:06:03 PST 2014


On Sunday, 23 February 2014 at 20:41:30 UTC, Jesse Phillips wrote:
> On Saturday, 22 February 2014 at 20:17:37 UTC, Frustrated wrote:
>> 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;
>>
>> }
>
> Why does your WindowsGui violate the iGui contract?

It doesn't. It simply that one can't specify dependencies in D

if (iGui is WindowsGui) then iButton is WindowsButton;

It's not very hard logic but people are not even trying.

I am attempting to make a mixin to solve the problem. The mixin
will simply overload all methods in the derived class(WindowsGui)
and when WindowsButton is used it will create an overload using
iButton(to satisfy the interface) with the check to make sure the
iButton is a WindowsButton.

It will give me what I want except I have to create the mixin and
then insert it in all the classes. (In theory though it should
not add any overhead is used where it is not suppose to)

But unfortunately when I try to find all members that use
WindowsButton(or whatever) to be able to create the new overload,
I get error due to the inner foreach.

		foreach (am; __traits(derivedMembers, B))
			foreach (m; [__traits(getOverloads, B, am)])
			{
                              // check if method contains an
parameter of type WindowsButton, then create identical method
definition that uses iButton instead. Put a check in the method
and call this method using a cast. This essentially overrides the
virtual method satisfying the interface but passes the work to
the user defined method.
			}

What the above code will do, when working, is create the verbose
code you quoted from the first case:

@property WindowsButton button(WindowsButton b)
{
}

The ***mixin*** turns **ADDS** this

@property WindowsButton button(iButton _b)
{
     if (is(_b : WindowsButton)) assert(0, "Rogue button used");
     auto b = cast(WindowsButton)_b;
     button(b); // Call the user defined function here(hopefully)
}


More information about the Digitalmars-d-learn mailing list