Callbacks or similar?
Jonathan M Davis
jmdavisProg at gmx.com
Sat Feb 16 13:29:51 PST 2013
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
More information about the Digitalmars-d-learn
mailing list