Static opCall for templated structs

Simen Kjaeraas simen.kjaras at gmail.com
Tue Apr 15 16:01:35 PDT 2008


I often want to offer users a pretty way to create the templated structs I  
use, and I find it complexifying to have to place such prettification  
outside the struct itself. For instance:

   struct foo(T)
   {
     T data;

     static foo!(U) opCall(U)(U u)
     {
       foo!(U) result;
       result.data = u;
       return result;
     }
   }

Seeing as the static opCall is T agnostic, it would be nice to be able to  
use it like:

   auto f = foo(4);

However, that does of course not work, I need to write:

   auto f = foo!(typeof(4))(4);

or place the static opCall outside the struct:

   foo!(T) Foo(T)(T t)
   {
     foo!(T) result;
     result.data = t;
     return result;
   }

   struct foo(T)
   {
     T data;
   }

Now, the first workaround is ugly, and the latter requires a different  
name for the constructing function, which is not quite as ugly, but still  
not as good as it should be.

...I'm starting to think this might be more of a feature request than a  
call for help, but oh well.

Is there a way to make this work in current D2.0, so that I can simply use  
the first syntax?

--Simen


More information about the Digitalmars-d-learn mailing list