arrayscoldfusioncfmlcoldfusion-2018

Typed array vs untyped array in ColdFusion


I have the following code:

<cfscript>
data = ['2342bas', 'asqwerewq', '12314', 12421, 1.1];

newdata = arrayNew['Numeric'](1);

for (item in data)  {
    newdata.append(val(item));
    }

writedump(newdata); 


newdata = [];

for (item in data)  {
    newdata.append(val(item));
    }

writedump(newdata);  
</cfscript>

I am getting the following results:

enter image description here

I am wondering why they are different. Does 'Number' force all the data to be floating point?


Solution

  • To get the answer I had to dive into the meta data

    <cfscript>
    data = ['2342bas', 'asqwerewq', '12314', 12421, 1.1];
    
    newdata = arrayNew['Numeric'](1);
    
    for (item in data)  {
        newdata.append(val(item));
        }
    
    newdata.each(function(value) {
        writeoutput("<br /><b>#value#</b> #getMetadata(value).getName()#");
        });
    
    writeoutput("<hr />");
    
    
    newdata = [];
    
    for (item in data)  {
        newdata.append(val(item));
        }
    
    newdata.each(function(value) {
        writeoutput("<br /><b>#value#</b> #getMetadata(value).getName()#");
        });
    </cfscript>
    

    Results

    enter image description here

    It is interesting that BigDecimal always has a decimal and Double may or may not. Based on this question, ColdFusion: Get variable type , I never knew their was a way to use a BigDecimal in ColdFusion.