digitalmars.D.learn - Callbacks or similar?
- Lemonfiend (36/36) Feb 16 2013 Hi,
- Dicebot (5/5) Feb 16 2013 http://dlang.org/expression.html#FunctionLiteral
- Lemonfiend (17/24) Feb 16 2013 You mean this?
- Jonathan M Davis (7/39) Feb 16 2013 foo([0, 1, 2], 1, &bar);
- Lemonfiend (4/47) Feb 16 2013 Ah you're right, it does, thanks.
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
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
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
On Saturday, February 16, 2013 22:24:19 Lemonfiend wrote:On Saturday, 16 February 2013 at 21:03:54 UTC, Dicebot wrote: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 Davishttp://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
On Saturday, 16 February 2013 at 21:31:05 UTC, Jonathan M Davis wrote:On Saturday, February 16, 2013 22:24:19 Lemonfiend wrote:Ah you're right, it does, thanks. I find the D documentation to generally be very hard to read :(On Saturday, 16 February 2013 at 21:03:54 UTC, Dicebot wrote: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 Davishttp://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