• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Feature for CF11, global LOCAL var scoping

Enthusiast ,
Oct 17, 2013 Oct 17, 2013

Copy link to clipboard

Copied

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.

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 17, 2013 Oct 17, 2013

Copy link to clipboard

Copied

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

<cfset var LOCAL = {

     'var1' = 'val1',

     'var2' = 'val2'

} />

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 18, 2013 Oct 18, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 18, 2013 Oct 18, 2013

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation