"Best" way of handling a receive()d message with a class instance.

Francesco Cattoglio francesco.cattoglio at gmail.com
Thu Jan 23 07:42:49 PST 2014


Suppose that I receive a message, but instead of defining a 
function inside the receive() block, I want to call a member of a 
class instance. (This is useful to me for several reasons). Right 
now my code looks like:

class Handler {
auto handle() {
     return (string msg) {
         writeln("received: ", msg, " count: ", i);
     }; // this ';' ends the return statement
} // handle()
private:
     i;
}

//somewhere inside main
auto handler = new Handler;
receiveTimeout( dur!"seconds"(1),
                 handler.handle
               );

It works, but I must say I'm not even sure about what the hell I 
am doing :D
If I got it correctly, I'm calling a function that returns a 
delegate, which is used by the receiveTimeout() somehow to 
dispatch the message (and everything is in some way decided at 
compile time, I believe).
Does this ugly piece of code make any sense? Should I rework it?


More information about the Digitalmars-d-learn mailing list