In your code, objects and collections can be instantiated using New object and New collection commands.
However, starting from 4D v20, a more convenient and readable approach is available using object literals and collection literals. These new features provide a simpler and clearer way to initialize objects and collections.
Object literal
An object literal creates a new empty or prefilled object:
- You can create an empty object using the {} expression:
$object1:={}
// is equivalent to:
$object2:=New object
- Or an object initialized with values utilizing a list of pairs of property names and associated values enclosed in curly braces:
$b:=42
$object1:={\
a:"foo"; \
b:$b; \
c:{}; \
d:["Hello"; "there"]\
}
// is equivalent to:
$object2:=New object(\
"a"; "foo"; \
"b"; $b; \
"c"; New object; \
"d"; New collection("hello"; "there"))
This new way to instantiate objects brings the advantage of creating the attributes as such and no longer as text. It allows you to:
- benefit from syntax highlighting
- use code completion to create attributes
- retrieve these attributes when you do a property search
COLLECTION literal
A collection literal creates a new empty or prefilled collection.
- You can create an empty collection using [] expression:
$collection1:=[]
// is equivalent to:
$collection2:=New collection
- Or a collection initialized with values:
$collection1:=[654; $b; {a:"foo"}]
// is equivalent to:
$collection2:=New collection(654; $b; New object("a"; "foo"))
We constantly strive to provide our users with the best possible experience, and we encourage you to share your thoughts and feedback on the 4D forum. Your feedback helps us better understand your needs and continuously improve our products and services. And think about consulting this feature in the documentation for more details!