suraj p Yedre
suraj p Yedre

Reputation: 21

How to use reserved keyword as variable in ColdFusion?

I have to use the reserved keyword "function" as variable name in ColdFusion. How can I do the same? I was trying with @ symbol but that didn't work. Any snippet will be helpful

Upvotes: 0

Views: 168

Answers (1)

Alex
Alex

Reputation: 7833

You didn't mention the context of the variable, but here are some observations and possible workarounds:

tag syntax

variable

<!--- works with ACF 10, 11, 2016, 2018 --->
<!--- works with Lucee 4+ --->
<cfset function = "function">
<cfoutput>#function#</cfoutput>

<!--- works everywhere --->
<cfset fieldName = "function">
<cfset variables[fieldName] = "function">
<cfoutput>variables[fieldName]</cfoutput>

function name

<!--- works everywhere --->
<cffunction name="function">
    <cfargument name="function">
    
    <cfreturn arguments.function>
</cffunction>

<!--- works with ACF 10, 11, 2016 --->
<!--- doesn't work in Lucee at all --->
<cfoutput>#function("function")#</cfoutput>

<!--- works everywhere --->
<cfinvoke method="function" returnVariable="x">
    <cfinvokeargument name="function" value="function">
</cfinvoke>
<cfoutput>#x#</cfoutput>

script syntax

variable

// doesn't work in ACF at all
// works with Lucee 5+
function = "function";
writeOutput(function);

// works everywhere
fieldName = "function";
variables[fieldName] = "function";
writeOutput(variables[fieldName]);

function name

// doesn't work in ACF at all
// works with Lucee 5+
function function(function) {

    return arguments.function;
}

// doesn't work in ACF at all
// doesn't work in Lucee at all
wrieOutput( function("function") );

// works in ACF
// doesn't work in Lucee due to a different bug
writeOutput( invoke("", "function", { "function": "function" }) );

In conclusion: Avoid using a reserved keyword! It's inconsistent and leads to preventable bugs.

Upvotes: 1

Related Questions