KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Better Readability with Collection and Object Literals
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: October 28, 2024

Introduced in 4D v20 alongside Object Literals, Collection Literals are a shorthand to make creating collections easier for developers. Likewise, Object Literals are a shorthand for creating objects. They can be used together to create collections that start with objects inside of them.

Not only is this a shorthand and thus less code to write, but one may notice the code is much more readable than the other way. The way attributes in object literals are inserted is neater to look at because this format allows 4D to color the attribute text into the color meant for attributes. Below are examples of an empty collection, a collection with objects, and a collection with dates done both ways for comparison.

var $col1; $col2; $col3; $col4; $col5; $col6 : Collection

//Collection and Object Literals:

$col1:=[]

$col2:=[{name: "Smith"; age: 25}; {name: "Jane"; age: 26}]

$col3:=[!2024-01-01!; !2023-07-04!]


//Instead of this way:

$col4:=New collection()

$col5:=New collection(New object("name"; "Smith"; "age"; 25); New object("name"; "Jane"; "age"; 26))

$col6:=New collection(!2024-01-01!; !2023-07-04!)