4D Blog

Home Product Simplify Variable Declarations & Assignments in a Single Line

Simplify Variable Declarations & Assignments in a Single Line

October 20, 2023

Product

Starting with 4D v20 R3, you can wave goodbye to the days of writing multiple lines of code for variable declarations and assignments. Now, you can streamline your code, eliminate redundancy, and easily boost efficiency.

Here is how!

Until now, we could write:

var <variableName> : <type>

We enhance this syntax to allow you to declare and initialize your variable in a single line.

inferred type

var <variableName> := <value>

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

Example:

//BLOB
var $vxBlob : Blob
INTEGER TO BLOB(0x0206; $vxBlob; Native byte ordering)
var $blob:=$vxBlob
//Boolean
var $bool:=True
//Null
var $null:=Null
//Real
var $num:=(569/2)
//Object
var $obj:={att1: 1}
//Pointer
var $point:=->$obj
//Text
var $text:="Hello"
//Collection
var $col:=[1; 2; 3]
//Date
var $date:=Current date
//Time
var $time:=Current time
//Picture
var $pict : Picture
var $image:=$pict

Remember that 4D tries to deduce the most general type when using this writing style. For example, 4D uses the Real type, not the Integer type, when a variable is initialized by 10. It allows you to use this variable with any type of calculation. If you wish to use an Integer, you must explicitly declare the type of your variable.

Declared Type and initialization

var <variableName> : <type> := <value>

Here, the variable is created with the given type and then initialized with the value.

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

var $myClass:={myAttribute: "Hello there!"}
var $myAttribute:=$myClass.myAttribute
// The interpreter knows the type of $myClass.myAttribute: $myAttribute is a declared as Text
// The compiler doesn't know the type of $myClass.myAttribute: $myAttribute is a declared as variant

So, dive into the documentation, explore this feature’s endless possibilities, and witness firsthand how this game-changing addition will shape the future of your coding endeavors.

Discuss

Tags 21, Language, Programming, v20 R3

Latest related posts

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

  • April 29, 2025

    Discover your AI-powered writing assistant in 4D Write Pro

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 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio 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 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio 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