4D Blog

Home Product Object and Collection Literal Initializer

Object and Collection Literal Initializer

April 26, 2023

Product

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!

Discuss

Tags Language, Programming, v20

Latest related posts

  • November 14, 2025

    Event Report in 4D Qodly Pro: See Every Interactions at a Glance

  • November 14, 2025

    4D Qodly Pro: Page Zoom Controls

  • November 13, 2025

    macOS Tahoe, Windows 11, which operating systems for 4D 20 and 21?

Fabrice Mainguené
Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • AI
  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy