Generically call a function on Variant's payload?

Paul Backus snarwin at gmail.com
Mon Aug 20 03:31:01 UTC 2018


On Monday, 20 August 2018 at 00:27:04 UTC, 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?

You are basically reinventing OOP here.

Instead of Variants, use objects that implement an interface 
(e.g., `std.range.interfaces.InputRange`). Then you can call 
methods that belong to that interface and rely on virtual method 
dispatch to choose the correct implementation at runtime.


More information about the Digitalmars-d-learn mailing list