Passing a function (with arguments) as function input

Ali Çehreli acehreli at yahoo.com
Sun Apr 22 21:10:47 PDT 2012


On 04/22/2012 04:19 PM, Joseph Rushton Wakeling wrote:
> Is there a way in which to pass a function as input to another function,
> with the arguments of the first function already determined?
>
> The case I'm thinking of is one where I have a function which wants to
> take a random number generation scheme, and use it on several occasions,
> without having any info on that scheme or its parameters.
>
> Here's a little test attempt I made:
>
> ////////////////////////////////////////////////////
> import std.random, std.range, std.stdio;
>
> void printRandomNumbers(RandomNumberGenerator)(RandomNumberGenerator
> rng, size_t n)
> {
> foreach(i; 0..n)
> writeln(rng);

You just need to call the delegate with the function call syntax:

           writeln(rng());

Ali


More information about the Digitalmars-d-learn mailing list