views:

49

answers:

1

I'm not even sure about how to title this qn. But, hope there is an easy way to do it in dynamic language like groovy.

say I have a class Service, where I delegate the business logic. the methods in it are funA(), funB()... funX().

Now I have a controller class, where I call the service closure, which can be invoked like service.funA() . Now based on a variable (which can have values A, B ... X), I need to cal the correct service closure. Basically to avoid writing lot of if conditional statements. Something like service."fun+var"() would do. I'm not sure whether it is possible to substitute variable in closure (function)name. or any way by passing function (name) as a parameter...not sure

I think PHP has a similar feature http://php.net/manual/en/functions.variable-functions.php

thanks for any pointer..

+4  A: 

Yes, this is possible. This should do what you want:

service."fun${var}"()

The correct title is dynamic method invocation.

Michael Borgwardt
Thank you so much.. worked great
bsreekanth