ColdFusion 2018 introduces a good set of enhancements on Language. In this port, we will be looking at all the feature enhancements around Arrays and Strings.
Typed Arrays
ColdFusion has always been a type-unaware language, with ColdFusion internally guessing the type of the object in order to perform appropriate actions on the object. ColdFusion 2018 addresses concerns on being more type friendly, while being backward compatible with two new features – Datatype Preservations and Typed Arrays. Datatype preservations ensure the type of each object is not lost – and is the subject for another post. This post covers Typed Arrays – which provisions to create arrays that hold elements of a specific type only.
Syntax: arrayNew[type]()
The type itself is specified in quotes – such as ‘String’ – to accommodate for variables that hold the type.
Example 1:
<cfscript>
// ['String'] specifies the type of the array
arr = arrayNew['String'](1);
arr.append("hello");
arr.append("world");
writeDump(arr);
</cfscript>
The following types are supported in a Typed Array:
- String
- Numeric
- Boolean
- Date / Datetime
- Array
- Struct
- Query
- Component – Accepts all instances of all components
- CFC – Accepts instances of the specified component, and instances of its sub-components.
- Binary
- Function
ColdFusion also attempts to cast appended array elements to the type specified. An exception is thrown when the type of the element being appended is not of the array type specified, and if ColdFusion is unable to cast the element to the specified type.
A literal syntax to Typed Array’s also exists,
[type][elem1, elem2, .. elemN]
Example 2:
<cfscript> // Literal syntax arr1 = ['String'][1, "Word1", "Word2"] writeDump(arr1) </cfscript>
In the above example, a typed array of type ‘string’ is created with literal syntax, and values 1, ‘Word1’, ‘Word2’ are inserted during initialization. The number 1, is also inserted since an automatic cast to a string type succeeds. However, the insertion of a string into a typed array of type ‘numeric’ would throw an exception.
Example 3:
comp.cfc
component { public string function A(){ return "A" } }
compDerived.cfc
component extends="comp" { public string function B(){ return "B" } }
<cfscript> obj1 = new component("comp") obj2 = new component("compDerived") arr2 = ["comp"][obj1, obj2] writeDump(arr2) </cfscript>
In the above example, a typed array of a specific component ‘comp’ is created with literal syntax. The typed array accepts all instances of the component ‘comp’ and ‘compDerived’, since ‘compDerived’ extends ‘Comp’.
Array Negative Indices
ColdFusion 2018 supports negative indices for querying arrays. Negative indices are incredibly useful when records need to be retrieved from a fag end of the array. With ColdFusion 2018, an index of -1 would return the last element of the array, -2 would return the last but one element of the array, and so on. Without negative indices, the only other way to get to the end of the array would be calculate the length of the array, and work backwards to get elements at the end of the array.
Eg:
<cfscript> animals = ['cat', 'dog', 'fish', 'bison']; writeOutput(animals[-1]); //gets the last element writeOutput(animals[-2]); //gets the last but one element </cfscript>
Array Slices
Array Slices, another introduction to array manipulations in ColdFusion 2018, provisions for more complex querying. With array slices, sub arrays can be queried without actually looping over every item in the array.
Syntax: array[ from : to : step ]
Arrays can now be queried with three parameters in place of the index. The resultant of such a query is a new array that satisfies the conditions denoted by the parameters. The first parameter, from, specifies the first index of the new array. The second parameters, to, specifies the last index of the new array, and step, the third parameters specifies the interval at which elements between ‘from’ and ‘to’ are listed in the new array.
All three parameters are optional, with the default values being [ 1 : < Array Length> : 1 ]. Negative Indices can be used in conjunction with Array Slices to query sub-arrays with elements in reverse.
Eg:
<cfscript> animals = ['cat', 'dog', 'fish', 'bison']; //All elements of the array writeDump(animals[:]); // Output: cat dog fish bison //All elements of the array writeDump(animals[1:-1]); // Output: cat dog fish bison //Elements from the first index, to the last but one //At a step of two - i.e., returning every alternate element writeDump(animals[1:-2:2]); // Output: cat fish //The array, in reverse writeDump(animals[-1:1:-1]); // Output: bison fish dog cat </cfscript>
String Literals and Member Functions
String member functions can now be directly called on string literals, i.e., strings are not necessarily required to be present in a variable to invoke string specific member functions such as FindNoCase() and Len().
Eg:
<cfscript> // Prior to ColdFusion 2018 str = " The quick brown fox jumps over the lazy dog " writeOutput("String Length: " & str.trim().len()) // In ColdFusion 2018 writeOutput("String Length: " & " The quick brown fox jumps over the lazy dog ".trim().len()) </cfscript>
This has the same documentation error as outlined in
https://tracker.adobe.com/#/view/CF-4203066
Namely, it is array.append() not array.add()
James,
We have fixed this in the docs.
You must be logged in to post a comment.