I have a dispatch mechanism which stores references to functions in a dictionary, and then calls the appropriate function by dictionary key. It works ok for functions with no parameters or a fixed number of parameters. For functions with variable arguement lists, I get the following exeption:
Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.
in <0x0004c> CompilerGenerated.___callable2:Call (System.Object[] args)
in <0x00120> Boo.Lang.Runtime.RuntimeServices:InvokeCallable (System.Object target, System.Object[] args)
in <0x002ac> TestModule:Main (System.String[] argv)
Note, code below is compiled with -ducky
"""
func1
func2 with arg: test
func3 param:multiple
func3 param:params
func1
func2 with arg: dispatcher arg test
func3 param:dispatcher
func3 param:multiple
func3 param:args
"""
def func1():
print 'func1'
def func2(arg1):
print 'func2 with arg: ' + arg1
def func3(*args):
for item in args:
print 'func3 param:' + item
func1()
func2('test')
func3('multiple', 'params')
dispatcher = {'f1': func1,
'f2': func2,
'f3': func3
}
dispatcher['f1']()
dispatcher['f2']('dispatcher arg test')
dispatcher['f3']('dispatcher', 'multiple', 'args')
dispatcher['f1'].Invoke()
dispatcher['f2'].Invoke('dispatcher arg test')
dispatcher['f3'].Invoke('dispatcher', 'multiple', 'args')
Another thing that works is wrapping the args in an array:
dispatcher['f3'](('dispatcher', 'multiple', 'args'))
But that only works for that one case.