Callbacks or similar?

Lemonfiend Lemon at fie.nd
Sat Feb 16 13:24:19 PST 2013


On Saturday, 16 February 2013 at 21:03:54 UTC, Dicebot wrote:
> http://dlang.org/expression.html#FunctionLiteral
>
> Function parameters/variables are declared in D using 
> "ReturnType function(ParameterTypes) symbol". "function" is a 
> keyword here, it can be swapped for "delegate" to get, em, 
> delegates.
>
> In your case something like "void function(int) callback" will 
> do.

You mean this?

void foo(int[] arr, int cmp, void function(int) callback)
{
	foreach(int val; arr)
	{
		if(val == cmp)
			callback(val);
	}
}

But how do I then pass bar to foo?

void bar(int val)
{
	writeln(val);
}

This doesn't work:

foo([0,1,2], 1, bar);


More information about the Digitalmars-d-learn mailing list