Generically call a function on Variant's payload?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Aug 20 02:23:32 UTC 2018


On Sunday, August 19, 2018 6:33:06 PM MDT Nick Sabalausky (Abscissa) via 
Digitalmars-d-learn wrote:
> On 08/19/2018 08:27 PM, Nick Sabalausky (Abscissa) wrote:
> > Suppose I've wrapped a Variant in a struct/class which ensures the
> > Variant *only* ever contains types which satisfy a particular constraint
> > (for example: isInputRange, or hasLength, etc...).
> >
> > Is there a way to call a function (ex: popFront) on the Variant,
> > *without* knowing ahead of time all the possible static types it might
> > might contain?
>
> Maybe something involving using Variant.coerce to convert the payload to
> a single common type? Not sure how I would do that though.

You could always create a wrapper type. Whatever code you're dealing with is
going to need to statically know what type it's using even that's a base
type in a class hierarchy. So, you need to present a type which actually has
the appropriate API even if internally, it can dynamically handle several
types which have that API, and it's not known ahead of time which type that
is.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list