Skip to main content
Inspiring
October 18, 2013
Question

Feature for CF11, global LOCAL var scoping

  • October 18, 2013
  • 2 replies
  • 1259 views

I've loved setting scopes in the following manner:

<cfset structAppend( VARIABLES, {

     'variableName'         = 'value',

     'variableName2'       = 'anotherValue',

     'variable'                  = 'yetAnotherValue'

}, true ) />

It allows me to set variables in 1 command using structure notation, and keeping the command in an easy to digest look.  But a problem I've run into is that I cannot do this with the LOCAL scope and also maintain the best practice of using the 'var' keyword to ensure the variables exist only for the duration of the function call.  For example, this currently fails:

<cfset structAppend( var LOCAL, {

     'variable1'     = 'value1',

     'variable2'     = 'value2'

}, true ) />

I was hoping the above would equate to the same thing as doing:

<cfset var LOCAL.variable1 = 'value1' />

<cfset var LOCAL.variable2 = 'value2' />

So I'd love to see CF11 support this.  I know it's menial, but I like using structAppend because it does not remove values that already exist in the scope, but if ones that already exist are provided as well, they are tastefull overwritten.

    This topic has been closed for replies.

    2 replies

    Participating Frequently
    October 18, 2013

    You could also do it like this:

    <cfset var LOCAL = {}>

    <cfset structAppend(LOCAL, {

         'variable1'     = 'value1',

         'variable2'     = 'value2'

    }, true ) />

    I'm not sure why you'd want to say "structAppend(var LOCAL".  Also just to complicate things the local scope in your functions you shouldn't need to create as a locally-var'd variable at all; it's in-built since 9.  So really you could just do:

    <cfset structAppend(LOCAL, {

         'variable1'     = 'value1',

         'variable2'     = 'value2'

    }, true ) />

    job done!  See http://forta.com/blog/index.cfm/2009/6/21/The-New-ColdFusion-LOCAL-Scope

    Inspiring
    October 18, 2013

    Whoa, never knew that.  That perfectly solves my problem.  Thanks for the knowledge bomb, Duncan!

    Inspiring
    October 18, 2013

    For the time being, I'm simply using a set.

    <cfset var LOCAL = {

         'var1' = 'val1',

         'var2' = 'val2'

    } />