On Thursday, 14 December 2017 at 11:36:46 UTC, mrphobby wrote:
I'm thinking it would be nice to wrap this setup code into some kind of mixin template so that I could put the required configuration setup in each class. In other languages like Python or C# I would maybe decorate my methods with an attribute and then use reflection to build the proper data structures at runtime. However, I'm completely new to D so it's a bit difficult to see a good solution here. If you have any ideas how to do this I'd appreciate it. Thanks for all help!

Also, it feels a bit awkward to implement the callback handling methods as static methods, with the "self" and SEL arguments. Would have been nice if it was possible to use instance methods.

After thinking about it for a while I guess it could work with one static dispatch method that maps selectors to specific instance methods. So when callbacks are made from Objective-C they are made to this one static method, which would then call the right method on the class.

I'm still struggling with D syntax, so here's some pseudo code:

class AppDelegate {
   static var methodMap = {}    // Maps selector name to methods

   static void handleCallback(AppDelegate self, SEL sel, ...) {
      var method = methodMap[sel];
self.call(method, va_list); // Call the method with args (not sure if possible in D)
   }

void applicationDidFinishLaunching(NSNotification notification) {
      // Normal instance method here
   }
}

Now, you would also need a registration step somewhere that sets up the selectors to use, perhaps in a static constructor that is run when AppDelegate class is used for the first time.

I hope this makes sense. Just throwing out some ideas :)

Reply via email to