Nifty chaining

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 4 16:07:19 PDT 2010


On Mon, 04 Oct 2010 09:52:21 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:


>      Chainer opDispatch(string fn, Args...)(Args args) if  
> (is(typeof(mixin("t." ~ fn ~ "(args)")) == T))
>      {
>          mixin("t." ~ fn ~ "(args);");
>          return this;
>      }

Ran into a huge snag here.  Because the functions being emulated are not  
templates, they do not do IFTI.  But opDispatch *does* do IFTI.

The issue is with literals.  Easily illustrated:

void foo(ushort x)
{
}

void wrappit(string fn, Args...)(Args args)
{
    mixin(fn ~ "(args);");
}

void main()
{
   foo(1); // ok
   wrappit!"foo"(1); // fails
}

The problem is, IFTI sees the "1" and instantiates "int".  But foo needs a  
ushort, and this doesn't work.

So how to fix this?  I have no idea.  The "type" of a literal is a  
polysemous type.  I wonder if there's some way the compiler could try an  
ordered list of types and see which one fits.  i.e. try to instantiate  
with int, if that doesn't work, try long, short, ushort, ulong, uint,  
etc.  Anything that "1" can be interpreted as. Or maybe it would be enough  
to just use the smallest type it could possibly use?

Can this work?  Would it take too long?

I lament for the day we can perfectly wrap functions...

-Steve


More information about the Digitalmars-d mailing list