Is it possible to handle 'magic' property assignments a'la PHP?

Artur Skawina art.08.09 at gmail.com
Sun Jan 5 08:54:30 PST 2014


On 01/05/14 15:36, TheFlyingFiddle wrote:
> Another simple example that have helped me tremendously when debugging OpenGL calls. A simple dispatcher that checks glGetError after every call.
> 
> struct GL
> {
>     auto opDispatch(string name, Args...)(Args args)
>     {
>         enum glName = "gl" ~ name;
>     mixin(format("
>         static if(is(ReturnType!%1$s == void))
>     {
>         %1$s(args);
>         checkGLError();
>     }
>     else
>     {
>         auto r = %1$s(args);
>         checkGLError();
>         return r;
>      }", glName));
> 
>     }
> }
> GL gl;
> 
> I simply use gl.funcName instead of glFuncName. opDispatch rules!.

While 'void' is not a first class type in D, there /is/ a special
case for returning 'void' from functions - so all of the above can
simply be written as:

   struct gl {
      static auto ref opDispatch(string name, Args...)(Args args) {
         scope (exit) checkGLError();
         return mixin("gl"~name~"(args)");
      }
   }

artur


More information about the Digitalmars-d-learn mailing list