d - Writing functions conforming template defined function prototypes -


to sheer delight discovered d has made progress , runs on windows 64 bit. has visual studio integration seems work far can see after few hours of playing new toy.

so, started play d language , bloody newbie in d.

in code below goal define function prototypes in template , write functions sub-functions of main() conform prototypes. compiler complains , right cannot find out how right (probably simple syntax problem).

the compiler errors are:

main.d(90): error: function main.actor!(int, int).worker (duration timeout, bool function(int) timeouthandler, bool function(int, int) messagehandler, int context) not callable using argument types ()

main.d(90): error: function main.main.w1ontimeout (int context) not callable using argument types ()

main.d(90): error: function main.main.w1onmessage (int context, int message) not callable using argument types ()

building debug\consoleapp1.exe failed!

import std.stdio; import core.time; import std.socket; import std.concurrency; import core.thread;  enum systemmessage {     shutdown,     terminate,     ping,     enable,     disable }  template actor(c,m)  {     // timeouthandler implementations return true      // unless worker shall terminate.     alias timeouthandler = bool function(c);     // messagehandler implementations return true      // unless worker shall terminate.     alias messagehandler = bool function(c,m);      void      worker         ( duration timeout         , timeouthandler timeouthandler         , messagehandler messagehandler         , c context         )     {         bool running = true;         bool enabled = true;         while(running)         {             if( true == std.concurrency.receivetimeout                     (timeout,                      (m message)                       {                          if(enabled)                          {                             if(!messagehandler(context,message) )                             {                                running = false;                              }                          }                      },                      (systemmessage message)                      {                            switch(message)                          {                              case systemmessage.shutdown: running = false; break;                              case systemmessage.terminate: running = false; break;                              case systemmessage.enable: enabled = true; break;                              case systemmessage.disable: enabled = false; break;                              case systemmessage.ping: /* todo: supervisor... */; break;                              default: break;                          }                      }                      ))             {             }             else             {                 if(!timeouthandler(context))                 {                     running = false;                 }             }         }     } }  alias intintactor = actor!(int,int);   int main(string[] argv) {     // signatures of next 2 functions conform function      // declarations, given in actor template (intintactor).      // how write them works?     bool w1ontimeout(int context)     {         writeln("w1ontimeout()");         return true;     }     bool w1onmessage(int context,int message)     {         writefln("w1onmessage: context = %d, message = %d", context, message);         return true;     }     // need special syntax here? e.g. &w1ontimeout ? or cast?     auto w1 = spawn(intintactor.worker,1000.msecs,w1ontimeout,w1onmessage,1);     for(int = 0; < 10; i++)     {         send(w1,i);     }     thread.sleep(5000.msecs);     send(w1,systemmessage.shutdown);     thread_joinall();     return 0; } 

thanks help, in advance!

you pretty gave answer in comment:

// need special syntax here? e.g. &w1ontimeout ? or cast? 

&w1ontimeout it; &intintactor.worker , &w1onmessage.

it still won't compile, because &w1ontimeout , &w1onmessage regarded delegates, worker takes function pointers. mark nested functions static , works:

static bool w1ontimeout(int context) {     ... } static bool w1onmessage(int context,int message) {     ... } 

alternatively, define them outside of main.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -