www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - polymorphic call from variant?

i had almost no problem in d-coding since i deferred creation of my 
messaging system. Although, i can't do that anymore - so, remembering what 
you told me, my expectations are much smaller now. This time i'd like to 
make 'basic' version, but STILL wasting hours on that o,o.
OK, so one most important thing i'd really need to make it at least usable 
(rather than make Msg struct + statically check which message has been 
sent...) is:

i got my client class like:

class Listener : IMsgListener!MsgWindowRefresh {
  mixin InjectMsgProvider!MsgWindowRefresh _prvdrRefresh;
  void handle(MsgWindowRefresh msg) {}

'interface' like:

interface IMsgListener(MSG) {
  mixin template InjectMsgProvider(MSG) {
    public bool register(IMsgListener!MSG what) {
    return MSG.mediator.registerListener(typeid(MSG), Variant(what));
  void handle(MSG);

So, basically i got everything - type, variant, fireworks :)
The only thing that would make it all work is:

void recvMsg(Variant msg) {
  foreach(ref l; _listeners[msg.type])

Unfortunately it's impossible because variant wants type as template 
argument. Which is a bit strange for me because it already have typeinfo BUT 
i suppose that author wouldn't make this template argument with no 
purpose... The thing i'm counting at: is there any slower :( solution to 

So, is there ANY way to call handle() methodS with concrete message rather 
than having to add:

switch (variant.type) {
case MsgA:
case MsgB:
case MsgC:

inside my Listener class?

In other words, can i call
via dynamic pointer(vtable) rather than static branching?

Mariusz GliwiƄski
Oct 07 2011