Search code examples
coldfusionrailocfmlcoldfusionbuilder

ColdFusion function variable name and CfBuilder


I need to call a function of an object and pass it a variable. Because I need to make multiple call to function of this object I've tried to make one only handler that invoke the specific function by the form value I pass it. The code works, but CFBuilder show me that there is an error (missing semicolon on the last row). I'm on Railo.

local.myReport = seoUtility.init();

local.func = form.action;

local.report = local.myReport[local.func](form.user);

So the question is: this code is correct? I could simply ignore the cfbuilder error icon?


Solution

  • If you don't want CFBuilder to nag you about the syntax, you can change to this:

    local.myReport = seoUtility.init();
    local.func = local.myReport[form.action];
    local.myReport.func = local.func;
    local.report = local.myReport.func(form.user);
    

    This sets local.func to the instance of seoUtility as a reference to the actual function you want to call, preserving its relationship to the parent object. This way the offending []() syntax isn't needed.

    However, this only works if seoUtility.init() is returning a fresh instance every time, as opposed to a singleton shared by the application, in which case there would be a race condition on all calls to local.myReport.func().