Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ListAppend non destructive, while ArrayAppend and StructInsert are both destructive?

Tags:

coldfusion

I just spent almost an hour trying to figure out an issue with having a list that would always return an empty string. I was using ListAppend just like one uses ArrayAppend or StructInsert, but apparently ListAppend works differently. What, if any, is the reasoning behind having ListAppend work differently from everything else?

<cfset ListAppend(list, item)>

list = ''

<cfset ArrayAppend(array, item)>

array[1] = item

<cfset StructInsert(struct, 'key', item)>

struct.key = item

like image 268
Phil Avatar asked Dec 09 '11 18:12

Phil


2 Answers

Possibly because a list is just a big String. Unlike arrays and structures, Strings are immutable, meaning they cannot be changed. To "append" a new value, you need to create an entirely new String. Arrays and structures are mutable. So you can modify them "in place".

like image 114
Leigh Avatar answered Nov 15 '22 17:11

Leigh


Lists in ColdFusion are just Strings and strings in ColdFusion (and Java) are immutable. They cannot be changed. So ListAppend() must return the a new string with the value instead of modifying the existing string.

<cfset newList = listAppend(oldList, "New Value") />
like image 31
Jason Dean Avatar answered Nov 15 '22 17:11

Jason Dean