Possible to do receive on a delegate?

Bienlein jeti789 at web.de
Wed Feb 19 12:16:01 PST 2014


On Wednesday, 19 February 2014 at 17:05:01 UTC, John Colvin wrote:
> delegate
>
> isn't a type. try
>
> int delegate()
>
> instead

Thanks for the hint! This works now:

void spawnedFunc(Tid tid)
{
      receive(
          (int i) { writeln("Received the number ", i);},
	 (int function() fp) { writeln("Receiving a function");}	
      );
};

int function() fp;

void main(string[] args)
{
	auto tid = spawn(&spawnedFunc, thisTid);

	int a = 7;
    	static int foo() { return 3; }
    	fp = &foo;

	tid.send(fp);
}

A delegate wouldn't compile. I guess it is because it could 
reference data outside the thread which would result in the 
thread reaching into memory of the calling thread.

I don't really understand why foo has to be static to compile. 
But this is really nice now :-).

-- Bienlein


More information about the Digitalmars-d-learn mailing list