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.