TypeInfoEx and Variant: suggestions?

Daniel Keep daniel.keep.lists at gmail.com
Thu Apr 16 17:48:25 PDT 2009



Christopher Wright wrote:
> Hi all,
> 
> I finally found some time to work on TypeInfoEx. It's usable, in that
> you can view information about a type; but you can't do anything with
> that information. One large barrier is the lack of a usable Variant type.
> 
> To invoke a function on an object or struct, you need to pass it as
> either Object or void* or Variant.
> 
> If you invoke a function, you want to pass in an array of Variant for
> the arguments. You want to get the return type as a Variant.
> 
> If you get a field, you want to get it as a Variant.
> 
> You could use templates instead, but that's not usable in many
> situations, so I won't have that as the only option.
> 
> Andrei's std.variant has an interesting design, but it has two flaws
> that render it unusable:
>  - It cannot be used with structs of arbitrary size. It's parameterized
> on size, but no size will always be sufficient; and due to its
> allocation scheme, choosing to support (for instance) 1KB structs will
> force a bool to take 1KB.
>  - It cannot be created with RTTI; it requires compile-time type
> information.
> 
> So, my options are:
>  - Write my own Variant supporting the necessary operations. This makes
> things annoying for anyone wanting to use my code with Phobos, since now
> there are two Variant structs running around.
>  - Use std.boxer, which is deprecated.
>  - Pass around an opaque Pair!(TypeInfo, void*).
> 
> Any suggestions? I've written such a Variant for D1 and Tango, but I
> relish neither the idea of supporting both with the same class, nor code
> duplication.

Use the Tango Variant code.  It supports values of any size and doesn't
require knowledge of the compile-time type.  It's in tango.core so the
dependencies are relatively small, so porting should be fairly simple
(the code originally compiled under both Phobos and Tango, but that
version's out of date now.)

If you look at http://dsource.org/projects/tango/ticket/821 there's a
patch to the current Variant that adds support for varargs as well.

  -- Daniel



More information about the Digitalmars-d mailing list