Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1057 (closed enhancement: fixed)

Opened 5 months ago

Last modified 1 month ago

Variant.get with interface that extends the inserted interface always fails

Reported by: dhasenan Assigned to: sean
Priority: major Milestone: 0.99.8
Component: Core Functionality Version: 0.99.5 Jascha
Keywords: triage Cc:

Description

dmd1.028, tango 0.99.5, linux

interface IFoo {}
interface IBar : IFoo {}
class Bar : IFoo {}

void main()
{
   IFoo foo = new Bar();
   Variant variant = foo;
   IBar bar = variant.get!(IBar); // throws VariantTypeMismatchException
}

Change History

04/17/08 12:23:49 changed by dhasenan

  • type changed from defect to enhancement.

Rereading the documentation, Variant.get only does implicit casting. I guess I'm changing this to a feature request for something like Variant.coerce that does the equivalent of an explicit cast. (Phobos has Variant.coerce, but its Variant is broken in other ways.)

04/17/08 12:28:38 changed by larsivi

  • milestone set to 0.99.7.

04/17/08 12:35:12 changed by dhasenan

Actually, you can get this with a one-line change. Variant.d:91 should read:

static if( is( T : Object ) || is( T == interface ) )

04/24/08 09:55:00 changed by dhasenan

My mistake, you also have to change line 279 to say:

this.value.obj = cast(Object) value;

(I saw this a few days ago, but no net access.)

05/09/08 17:45:03 changed by larsivi

I guess I'm a bit dim here - in which cases would such a coerce be of value? In your example, the object doesn't actually implement the IBar interface.

And are you proposing a coerce member, or that get should get this functionality?

05/24/08 13:07:54 changed by larsivi

  • keywords set to triage.

07/10/08 07:01:24 changed by larsivi

  • milestone changed from 0.99.7 to 0.99.8.

07/26/08 12:49:43 changed by sean

  • status changed from new to closed.
  • resolution set to wontfix.

I'm not sure I like the idea of having Variant support automatic downcasting. In this instance, I think it's probably better to simply do the downcast outside the variant:

IBar bar = cast(IBar) variant.get!(IFoo);

This is more clear to me because the line states explicitly "there is an IFoo in here but I'd like to treat it as an IBar." With automatic downcasting, a maintainer of the code may never know that the variant holds an IFoo and not an IBar.

07/26/08 13:17:07 changed by sean

  • status changed from closed to reopened.
  • resolution deleted.

My mistake. Variant already supports downcasting from an interface to a class, just not from an interface to an interface. This is clearly broken. I'll fix it.

07/26/08 15:33:58 changed by sean

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [3810]) Added support for storing and retrieving interfaces. This closes #1057