D equivalent of C++ bind ?

chmike via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 10 02:39:53 PDT 2016


Is there an equivalent in D of the C++11 std.bind template class 
[http://en.cppreference.com/w/cpp/utility/functional/bind] ?

Here is a blog post showing different examples of its use
https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/


A possible use case is for a callback function/delegate with the 
expected signature bool cb(int error). I would like to pass a 
function bool myCb(int error, ref int myArg) instead with the 
variable myArg being given as predefined argument.

Here is an example.
----
int count = 0;

bool myCb(int error, ref int myArg)
{
     if (myArg >= 6)
         return false;
     writeln(++myArg);
     return true;
}

void async_task(void function(int error) cb) { . . . while cb(0) 
. . . }

void main() {
     . . .
     async_task( ??? myCb ??? count ??? );
     . . .
}
----

In C++ we would write

async_task(std::bind(myCb, std::placeholders::_1, count));






More information about the Digitalmars-d-learn mailing list