Generically call a function on Variant's payload?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Mon Aug 20 03:34:19 UTC 2018
On Sunday, August 19, 2018 9:08:39 PM MDT Nick Sabalausky (Abscissa) via
Digitalmars-d-learn wrote:
> On 08/19/2018 10:23 PM, Jonathan M Davis wrote:
> > On Sunday, August 19, 2018 6:33:06 PM MDT Nick Sabalausky (Abscissa) via
> >
> > Digitalmars-d-learn wrote:
> >> 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.
>
> I guess the parts I'm unclear on are:
>
> 1. What mechanism does Variant.coerce!SomeType use to attempt conversion
> to SomeType?
>
> 2. How (if possible) can I provide a way to convert something to type
> SomeType which Variant.coerce!SomeType will then recognize and use?
Glancing at coerce's implementation, it just uses std.conv.to, but it's
fairly restricted on which types it will convert to. The target type has to
be numeric, bool, convertible to Object, or an array of characters. It looks
like it will convert the object to string to do the conversion, so it's
doing slightly more than just using std.conv.to and restricting the
conversions, but really, it's basically just calling std.conv.to in a
restricted manner.
So, looking at coerce, I don't really understand why it exists. It just
seems like it would make more sense to tell folks to get the object out with
get and then convert it themselves using std.conv.to or whatever makes the
most sense for their use case. Presenting a function that uses std.conv.to
but restricts which conversions work really doesn't make sense to me.
Either way, if you're doing something like using a Variant to hold multiple
range types, I very much doubt that coerce is going to do you much good.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list