[Issue 2073] Variant.coerce!() fails

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Feb 6 19:43:38 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=2073



--- Comment #2 from Michael Rynn <y0uf00bar at gmail.com> 2010-02-06 19:43:37 PST ---
Plus once we get a valid object , an extra template is needed in std.conv for
interfaces for the Interface coercion to work, just like the Object-to-Object

For std.conv

/**
Object-to-interface conversions throw exception when the source is
non-null and the target is null.
 */
T to(T, S)(S value) if (is(S : Object) && is(T == interface))
{
    auto result = cast(T) value;
    if (!result && value)
    {
        throw new ConvError("Cannot convert object of static type "
                ~S.classinfo.name~" and dynamic type "~value.classinfo.name
                ~" to type "~T.classinfo.name);
    }
    return result;
}

And it would be nice for the unittest to reflect the increased capability.

unittest {
           interface A {}
            interface Z {}

            interface B : A {}
            class C : B {}
            class D : C {}    
            class E : Z {}


            string K = "123";

            Variant vn1 = K;

            int n1 = vn1.coerce!(int);

            assert(n1 == 123, "failed string to number coercion");

            A a = new D;
            Z x = new E;
            Variant v2 = a;
            //B b =  v2.coerce!(B);
            C c = cast(C) a;

            a = v2.coerce!(A);

            c = v2.coerce!(C);
            D d = v2.coerce!(D);

    try {
       x = v2.coerce!(Z);
    }
    catch( std.conv.ConvError ce)
    {
    //writeln("Convert error");
       x = null;
    }
    assert(x is null, "invalid coercion");
}

Issues.
--------
Should interface ref back to object ref be allowed?
Should there be a facility to do this from a "generic" interface pointer.
Attempts to do it using higher level cast thingos failed.  

There is no generic cast(Interface) like cast(Object), and we know the pointers
are not the same sort of thing.

How about something like eg,  interface(<ptr>).toObject ?, instead of exposing
the icky pointer internals.  (Also object.d and cast_.d seem to differ on how
it is done. cast.d looks like it needs a makeover)

Such a facility implicitly exists to get a this reference into every interface
virtual method call.

Having to know about TypeInfo_Class and TypeInfo_Interface, and Interface*
offsets is icky.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list