www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Callbacks or similar?

reply "Lemonfiend" <Lemon fie.nd> writes:
Hi,

I'm looking for a way to do call different functions within a 
loop, so I won't have to write multiple functions with the same 
looping mechanism.
I'm not sure if I'm explaing this very well, but this very simple 
example should clarify:

void foo(int[] arr)
{
	foreach(int val; arr)
	{
		switch(val)
		{
			case 0:
				// call bar(val)
			break;

			case 1:
				// call barOther(val)
			break;

			default:
				throw new Exception("foo");
		}
	}
}


Now, this works, but it doesn't have my preference. I would 
prefer to use something like this:

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


How can I do this in D?
Or, is there a better way?

Thanks :)
Feb 16 2013
parent reply "Dicebot" <m.strashun gmail.com> writes:
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.
Feb 16 2013
parent reply "Lemonfiend" <Lemon fie.nd> writes:
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);
Feb 16 2013
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, February 16, 2013 22:24:19 Lemonfiend wrote:
 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);
foo([0, 1, 2], 1, &bar); should work. void function(int) is a function pointer, so you need to pass it a pointer to a function. bar by itself is attempting to call the function (which won't work due to a lack of arguments). It's discussed in the link to the documentation that Dicebot gave you. - Jonathan M Davis
Feb 16 2013
parent "Lemonfiend" <Lemon fie.nd> writes:
On Saturday, 16 February 2013 at 21:31:05 UTC, Jonathan M Davis 
wrote:
 On Saturday, February 16, 2013 22:24:19 Lemonfiend wrote:
 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);
foo([0, 1, 2], 1, &bar); should work. void function(int) is a function pointer, so you need to pass it a pointer to a function. bar by itself is attempting to call the function (which won't work due to a lack of arguments). It's discussed in the link to the documentation that Dicebot gave you. - Jonathan M Davis
Ah you're right, it does, thanks. I find the D documentation to generally be very hard to read :(
Feb 16 2013