Dispatching on a variant

Robert Jacques sandford at jhu.edu
Sat Sep 26 09:14:55 PDT 2009


On Sat, 26 Sep 2009 10:36:06 -0400, Justin Johansson  
<procode at adam-dott-com.au> wrote:
> language_fan Wrote:
>
>> Sat, 26 Sep 2009 09:32:55 -0400, Justin Johansson thusly wrote:
>>
>> > I've had a good poke around the forums and couldn't find anything on
>> > this so ...
>> >
>> > What's the recommended method for dispatching code off the runtime  
>> type
>> > of a variant variable (Phobos D2 std.variant)?
>> >
>> > Does one use a bunch of
>> >
>> > if ( var.peek!(type1)) { ... }
>> > else if ( var.peek!(type2)  { ... }
>> >
>> > for all N possible types, or is there a better & faster way with a
>> > switch or jump table of sorts?
>>
>> If the type count gets large, how fast it is depends on the backend
>> optimizations of the compiler. In the worst case it is a O(n) time  
>> linear
>> search. A jump table or almost any other way of dispatching would be
>> faster. If the variant had an integral tag field, it could be used in a
>> switch; that way the compiler could easily optimize it further with the
>> currently available constructs.
>>
>> This problem is solved in higher level languages by providing pattern
>> matching constructs. The compiler is free to optimize the code the way  
>> it
>> likes:
>>
>>   case var of
>>     type1 => ...
>>     type2 => ...
>>     ...
>>
>> But since no C-like language has ever implemented pattern matching, it
>> might be too radical to add it to D.
>
> Thanks both for replies.
>
> I've got about 2 dozen types in the variant so the O(n) really hurts.
> The variant thing seemed like a really cool idea at the time but now ...
> Without something like suggested above or a computed goto on typeid or  
> Andrei's visitator,
> it almost pushes me to backout from using variants and having to  
> redesign around some common base class or interface and using virtual  
> function dispatch. :-(
>
>
>

Here's an idea. Since you know each type at compile time, you can generate  
the list of methods they implement + a private set of delegates for their  
implementation. Then on assignment of a type, variant would simply assign  
the correct delegate for each supported method and an exception throwing  
delegate for un-supported methods.



More information about the Digitalmars-d mailing list