www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Accepting delegates/functions in templates

reply =?UTF-8?B?Ikx1w61z?= Marques" <luismarques gmail.com> writes:
I'm trying to accept delegates or functions in a templated method.

My first try using "isDelegate!dg || isFunctionPointer!dg" did 
not work because isDelegate fails, although I'm not sure I 
understand exactly why:

     void main()
     {
         int x;
         static assert(isDelegate!(() { x = 4; }));
         // Error: static assert ... is false
     }

If I changed that example to use isSomeFunction it works 
correctly. But when I tried to use isSomeFunction in the proper 
context it fails to compile:

     class A
     {
         void foo(alias dg)()
             if(isSomeFunction!dg)
         {
         }
     }

     void main()
     {
         int x;
         A a = new A;
         a.foo!((int param) { x = 4;});

         // error: cannot use local '__lambda1' as parameter to 
non-global
                       template foo(alias dg)() if 
(isSomeFunction!(dg))
     }

Can anyone help me understand this?
May 27 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/27/2013 07:05 AM, "Luís Marques" <luismarques gmail.com>" wrote:

 If I changed that example to use isSomeFunction it works correctly. But
 when I tried to use isSomeFunction in the proper context it fails to
 compile:
It is a red herring. The code fails to compile even without isSomeFunction. import std.traits;
      class A
      {
          void foo(alias dg)()
              if(isSomeFunction!dg)
          {
          }
      }

      void main()
      {
          int x;
          A a = new A;
          a.foo!((int param) { x = 4;});

          // error: cannot use local '__lambda1' as parameter to 
non-global
                        template foo(alias dg)() if (isSomeFunction!(dg))
      }

 Can anyone help me understand this?
It is an unsolved technical issue about delegates' currently not having sufficient number of context pointers: http://d.puremagic.com/issues/show_bug.cgi?id=5710 Ali
May 27 2013