Hi Merlo,

On 29.06.22 18:53, Merlo, Jason wrote:
A separate function can be registered for each input, however I would need to dynamically generate functions based on the number of inputs a user has configured, which has been giving me trouble. Ideally, the function handler callback would provide auxiliary arguments to pass to the callback function as arguments that can be bound at instantiation (such as index), but I don't think this is possible.

It definitely is! It's what lambdas are for.
I've written a small illustration for you: https://gcc.godbolt.org/z/cssn8hMno

Here, "float divide(int, int)" is your function that takes an extra argument (in case of your universal message handler, that might be the name of the handler).

with [capture](arguments) { return something; } you can write an anonymous function (a lambda) that "knows" its "captured" values, and still takes arguments.


Presently, I've looked into the functools package |partialmethod| to create separate instances of the |process_msg(self, msg, idx)| method with the |ch_idx| argument set, effectively creating |process_msg_idx(self, msg)| dynamically based on the number of inputs the user desires like:

Oh, this is python. Makes it a bit easier;

handler_name = something
lambda argument: underlying_function(argument, handler_name)

curries underlying function to have its second argument already set, i.e. down to a function of a single variable.

Best regards,
Marcus

Reply via email to