I understood the concept of delegates very well. I really don't understand why I cannot just pass one function to another, and I need to pass it to the delegate. I read in the documents that there are some cases where I do not know his name, and a delegate is only a way to call him.
But now it’s hard for me to understand the concept of callbacks. I tried to find more information, but I can’t understand if it’s just a call to another function or what it is.
Could you show examples of D callbacks and explain where they can be useful?
import vibe.d;
shared static this()
{
auto settings = new HTTPServerSettings;
settings.port = 8080;
listenHTTP(settings, &handleRequest);
}
void handleRequest(HTTPServerRequest req,
HTTPServerResponse res)
{
if (req.path == "/")
res.writeBody("Hello, World!", "text/plain");
}
&handleRequest
is it a callback? How does it work and at what point does it start?
source
share