Why opDispatch uses SFINAE implicitly?

Dukc via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 2 04:38:39 PST 2017


On Thursday, 2 March 2017 at 12:05:44 UTC, Dukc wrote:
> I still have to test if I can get that catch-all error-printing 
> opDispatch to work...

I encountered a problem: it was not SFINAE after all. If the 
compiler detects two canditates it won't try to use either of 
them, it just silently ignores opDispatchs again.

auto ref opDispatch(string Op, Args...)(Args arguments)
{   static if(is(typeof(_opDispatch!Op(arguments)))) 
_opDispatch!Op(arguments);
     else
     {   import std.conv;
         pragma(msg,
             "opDispatch didn't compile at "
         ~   __FILE__
         ~   " "
         ~   __LINE__.to!string
         );
         assert(false);
     }
}

This works, but is annoying to use:
-It is clunky to define. Could be of course made better with a 
mixin.
-It does not abort compilation. If I use a static (dis)assert, 
that's a compilation error which again causes ignorance of the 
whole implementation.
-You have to manually replace the call dispatched with a direct 
_opDispatch call and recompile to get the real error behind.

But is it the best option?


More information about the Digitalmars-d mailing list