Forcing opDispatch error (not SFINAE?)

via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 17 13:13:38 PDT 2014


Consider this:

     struct S
     {
         void opDispatch(string name, T)(T value)
         {
             alias AllowedTypes = SomeComplexTemplate!S;

             //static assert(is(T : AllowedTypes, "wrong type"));

              static if(!is(T : AllowedTypes))
             {
                 pragma(msg, "wrong type");
                 static assert(0);
             }

         }
     }

If the static assert fails, then the opDispatch instantiation 
fails, and so the relevant member is declared not found. Because 
of SFINAE, I assume (no?), the user/developer will never see the 
"wrong type" error message. So, instead, you have to use a 
pragma(msg), saying something like "hey, ignore the message 
bellow saying 'name' is not found; it's totally there, the 
problem is that you tried to assign a value of an invalid type".

Is there a way to improve this situation?

(BTW, the documentation for opDispatch is a bit thin on the 
details.)


More information about the Digitalmars-d mailing list