Library writing - delegates or fp's?

Chad J gamerChad at _spamIsBad_gmail.com
Tue Jun 20 23:12:54 PDT 2006


Derek Parnell wrote:
> 
> It doesn't have to be as complex as that. All you need to do to turn
> module-level functions into delegates is to place them inside the module's
> constructor or any other function for that matter, including main(). For
> example ..
> 
>  import std.stdio;
>  int delegate(int) increment;
>  static this()
>  {
>     int incr( int a )
>     {
>          return a + 1;
>     }
>     increment = &incr;
>  }
> 
>  void main()
>  {
>     writefln( increment(1) );
>  }
> 

Pretty nice, but then I can't do something like this:

import std.stdio;

int delegate(int) increment;

void main()
{
     writefln( increment(1) );
     writefln( incr(2) );
}

static this()
{
     int incr( int a )
     {
         return a + 1;
     }

     increment = &incr;
}

I'm thinking incase someone wanted to say, have a mouse handling 
function be triggered by the GUI's delegate and also have it 
artificially triggered by some other code via a direct function call 
(maybe a tutorial, debug routine, or something like that).

I suppose they should just call the delegate though.  I like it.  Thanks.



More information about the Digitalmars-d-learn mailing list