Simplify Variable Declarations & Assignments in a Single Line

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.


var $vxBlob : Blob
INTEGER TO BLOB(0x0206; $vxBlob; Native byte ordering)
var $blob:=$vxBlob
var $bool:=True
var $null:=Null
var $num:=(569/2)
var $obj:={att1: 1}
var $point:=->$obj
var $text:="Hello"
var $col:=[1; 2; 3]
var $date:=Current date
var $time:=Current time
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.

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.