4D Blog

Home Product Class property declarations and assignments on a single line

Class property declarations and assignments on a single line

May 7, 2024

Product

In 4D 20 R5, the streamlined property declaration now offers the convenience of initializing properties simultaneously with their declaration, akin to variable assignment.

This enhancement simplifies code readability and efficiency by allowing developers to declare and initialize properties in a single line. This facilitates smoother integration and reduces the need for separate initialization blocks. It optimizes development workflows and ensures code robustness across compilers and interpreters, promoting cleaner, more maintainable codebases.

Until now, there was only one way to create a class property:

property <propertyName> : <type>

Starting with 4D 20 R5, we enhance the property declaration. As for variables, you can now declare and initialize your property on one line, as described below:

inferred type

property <propertyName> := <value>

Here, the type is omitted and will be determined by the compiler or interpreter based on the provided value. To prevent compilation errors, it is advisable to employ this approach exclusively with scalar types originating from values, commands, or methods.

Example:

//BLOB
property myBlob:=GetBlob()
//Boolean
property myBool:=True
//Null
property myNull:=Null
//Real
property myNum:=(569/2)
//Object
property myObj:={att1: 1}
//Text
property myText:="Hello"
//Collection
property myCol:=[1; 2; 3]
//Date
property myDate:=Current date
//Time
property myTime:=Current time
//Picture
property myPicture:=GetPicture()

// Properties must be declared before class constructor or functions. 
Class constructor

Declared Type and initialization

property <propertyName> : <type> := <value>

As for the variable declaration, the property is created with the given type and then initialized with the value.

This declaration type must be used when you initialize a property with a type like a class attribute, an interprocess variable, or a global one. If you don’t specify the type in these cases, the interpreter and the compiler will evaluate the won’t be able to determine the type and will use a variant type instead. For example:

property myAttribute:=JSON Parse(GetJson()).myAttribute
// The interpreter and the compiler don't know the type of JSON Parse(GetJson()).myAttribute.
// The myAttribute property is declared as a variant

In 4D 20 R5, property declaration in a single line streamlines initialization, enhancing code readability and efficiency. This feature, detailed in the documentation, allows for seamless integration, optimizing development workflows and ensuring code robustness.

Discuss

Tags 20 R5, 21, Language, Programming

Latest related posts

  • July 16, 2025

    Recursive Dependency Management: Smarter, Deeper, Safer

  • July 16, 2025

    Stricter class-based typing for objects

  • July 16, 2025

    ORDA – Get started with the touched event

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

  • 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-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Data explorer Formula Google Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Data explorer Formula Google Listbox Logs Mail Microsoft 365 Network Objects 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