With 4D v16 R4, we introduced Object Notation which is a great enhancement to the 4D language: your code is fast, flexible, efficient and elegant! This feature created a lot of interest on the 4D forums and we received a lot of feedback from our customers highlighting situations when there were undefined values making the code challenging to write.
With 4D v16 R5, we have taken your feedback into account and have simplified the use of undefined properties and values with Object Notation.
This accomplishment consists of two parts: first, the 4D language is now accepting undefined values anywhere in an expression using object notation. Secondly, whenever you assign an undefined value to a variable, the variable is now set to a default value according to its type (0 for numeric, “” for string, etc.).
Read an undefined property or value
In prior versions, reading the property of a non-existing object generated an error and stopped the code execution. Now in 4D v16 R5, it produces an undefined value.
Let’s look at an example where you want to check the existence of the object property p2 in $o.p1.p2 and you are not sure that the $o.p1 object is defined:
In 4D v16 R4, you should first check if the p1 property is defined and then do the same for p2:
C_OBJECT($o)
$o:=InitObject
If ($o.p1#Null) // Check if p1 is defined
If ($o.p1.p2#Null) // Check if p2 is defined
$o.p1.p2.p3:="some text"
In 4D v16 R5, you can simply write the code below … even if p1 is undefined.
C_OBJECT($o)
$o:=InitObject
If ($o.p1.p2#Null) // Check immediately if p1 and p2 are defined
$o.p1.p2.p3:="some text"
ConverT undefined valueS
TYPE conversion commands NOW accept undefined values
The String, Num, Date, and Time commands now accept undefined values and return a default value according to the type. To complete this command set we have created a new Bool command, which returns False when you pass an undefined value. This update is especially useful when calling 4D commands.
For instance, if a 4D command expects a text expression as a parameter, you cannot pass an undefined value. Thus, you have to write this type of code in 4D v16 R4 and verify if the property exists before using the 4D command:
C_OBJECT($o)
$o:=InitObject
If ($o.p1#Null) // Check if p1 is defined
If ($o.p1.p2#Null) // If p1 is defined, you can now check if p2 is defined
READ PICTURE FILE($o.p1.p2;$i)
else
READ PICTURE FILE("";$i)
End if
End if
In 4D v16 R5, you can just write the code below. The String command checks and converts the undefined value for you. Now, you only need a single line of code instead of four!
C_OBJECT($o)
$o:=InitObject
// If $0.p1.p2 is undefined, String return "" and READ PICTURE FILE command opens standard Open file dialog box.
READ PICTURE FILE(String($o.p1.p2);$i)
Assigning an undefined value
From now on, if you assign an undefined value to a variable, the variable is set to its default type value (0 for a numeric, “” for a string, etc.).
C_LONGINT($l)
$l:=10
$l:=$o.undefined
// In 4D v16 R4, $l does not change, $l=10
// In 4D v16 R5, $l is reinitialized, $l=0
In fact, you can now pass an undefined value as a parameter to a project method, too. It works exactly the same way. The value is immediately converted according to the declared parameter type and set to its default type value.
Finally, you can also pass undefined values to Case of and If statements. In this case, 4D considers an undefined value as False:
If ($o.b)
// if $o.b is undefined this code is not executed
...
End if
For more details, you can take a look at the Using object notation article.