|
|
|
|
|
- Overview
- Allowing nullable types
- Functionalities associated with the activation of nullable types
- Storing null values in a variable
- Overview
- Nullable simple type
- Determining if an element is null
- Getting the value of an element that allows null values
- ?! operator
- The?? (coalescence operator)
- ??* operator
- ??= operator
- ??=* operator
- Using null values in WLanguage functions
- Special case: Serialize / Deserialize
- Special case: prefix syntax
- Using null values in procedures
- Passing parameters by value
- Return value
- Operations available on variables that allow null values
- Arithmetic operations
- Comparison operations
- Boolean operations
- Conditions
NULL values: Allowing nullable types
Starting with version 2024, you can allow nullable types in a WINDEV, WEBDEV or WINDEV Mobile project. Allowing nullable types in a project changes the way your project handles null values. This help page presents the various functionalities associated with this option. Allowing nullable types To allow nullable types: - Open the project description window. To do so, go to the "Project" tab, "Project" group, and click "Description".
- On the "Compilation" tab, check "Allow nullable types".
- A message about the implications of enabling this option appears. Click "Enable".
- Validate the project description window.
Functionalities associated with the activation of nullable types When the "Allow nullable types" option is selected, a series of functionalities are enabled. These features change the way null values are handled: Storing null values in a variable Overview When "Allow nullable types" is enabled, there are certain criteria to assign Null constants to simple variable types. In this mode, simple types (int, string, date, etc.), called non-nullable types, do not allow null values. Note For complex types (object, array, etc.), the behavior is unchanged: a complex type can be Null. To use the Null constant with simple types, it is now necessary to use nullable simple types. Nullable simple type To define a nullable simple type, use one of the following syntaxes: - add a question mark ("?") after the name of the type.
<Variable name> is <Type>? For example:
n is int?
s is string Unicode?
n1 is 8-byte int?
- use the <nullable> extension attribute.
<Variable name> is <Type> <nullable> For example:
Remarks: - These syntaxes are only available if the "Allow nullable types" option is enabled.
- The default value of a nullable type is Null (and no longer the type's initial value).
- Type inference is available for nullable types. For example:
LET x = ProcedureReturnNullableInteger()
The following simple types can be nullable:
Determining if an element is null When the "Allow nullable types" option is enabled, the result of a comparison of an element with the Null constant changes, as follows: - a simple non-nullable type can be compared with the constant Null constant: the result always corresponds to False.
- a simple nullable type can be compared with the constant Null constant: the result corresponds to True if its value is Null, False otherwise.
- a complex type that allowed the value Null in previous versions (object, array, etc.) doesn't change its behavior: it can still be compared with the value Null.
Remarks: - HFSQL field If the data file and the field allow the value Null, it is now possible to know if an HFSQL field is Null by comparing it directly to the constant Null. Example:
IF Fichier.RubriqueAvecNull = Null THEN
END
- Edit control field If the "Return NULL if empty" option is checked for the field, it is possible to find out whether an Edit control has returned Null.
Getting the value of an element that allows null values When you enable the "Allow nullable types" option, you need to check the code used to get the value of an element that allows null values. Assigning the value of a nullable element to a nullable element is perfectly fine. The Null value is preserved, if necessary. However, assigning a nullable element to a non-nullable element causes a compilation error. For example, the following lines of code are not allowed: s is string = Fichier.RubriqueNullable
ou Left(Fichier.RubriqueNullable)
You need to specify how Null values should be handled, directly in the line of code. To do this, the WLanguage provides several operators for accessing the values of elements that allow the Null value: ?!, ??, ??*, ??= and ??=*. ?! operator If it is certain that the value will be non-null, you can use the ?! operator. The compilation error related to the use of a nullable element will not be displayed in this case. Example: IF Fichier.RubriqueAvecNull <> Null THEN
s is string = Fichier.RubriqueAvecNull ?!
END
Reminder: If the code is erroneous and the value is Null at runtime, an exception will be raised by the WLanguage security mechanism. Note: In many cases, the?! operator is automatically applied to simplify code writing: - on elements that cannot be determined at compile time: query results, indirection, etc.
- in certain conditional syntaxes. For example:
IF Fichier.RubriqueAvecNull <> Null THEN
s is string = Fichier.RubriqueAvecNull
END
Warning: automatic application of the?! operator does not dispense with handling the case of the Null value. Otherwise, an exception will be thrown at runtime. The?? (coalescence operator) The ?? operator allows you to get: - the value of a nullable element if this value is non-null,
- a substitution value, if the nullable element is set to Null.
Syntax: nullable_expression?? substitution_value Example:
s is string = Fichier.RubriqueAvecNull ?? "N/A"
??* operator The ??* operator allows you to get: - the value of a nullable element if this value is non-null.
- the default value if the type, if the nullable element is set to Null.
Example:
s is string = Fichier.RubriqueAvecNull ??*
??= operator The ??= operator makes it possible to directly assign a substitution value to a nullable element that holds the Null value. This operator is only available for variables or items in data files. Syntax: nullable_element??= substitution_value Example: valeur_nullable is string?
valeur_nullable = ProcédureAExécuter()
valeur_nullable ??= "N/A"
This is more concise than the code below: valeur_nullable is string?
valeur_nullable = ProcédureAExécuter()
IF valeur_nullable = Null THEN
valeur_nullable = "N/A"
END
??=* operator The ??=* operator makes it possible to directly assign a default value to a nullable element that holds the Null value. This operator is only available for variables or items in data files. Example: valeur_nullable is string?
valeur_nullable = ProcédureAExécuter()
valeur_nullable ??=*
This is more concise than the code below: valeur_nullable is string?
valeur_nullable = ProcédureAExécuter()
IF valeur_nullable = Null THEN
valeur_nullable ??*
END
Using null values in WLanguage functions To simplify debugging, Trace displays null values or nullable types: - if the value is Null, the "<Null>" string is displayed.
- if the type is nullable but does not hold a Null value, the "<Nullable>" string is displayed next to the value.
Starting with version 2024 Update 2, you can use TypeIsNullable to determine if a type is nullable. Note Many WLanguage functions allow Null values and work as before ( TreeAdd, TreeModify, etc.). Special case: Serialize / Deserialize - In a binary deserialization, nullable members are set to Null if they do not exist in the serialized buffer.
- In an XML serialization, nullable members that hold the Null value are not serialized. During deserialization, members are set to Null if they do not exist in the XML content.
- In a JSON serialization, nullable members that hold the Null value are not serialized. During deserialization, members are set to Null if they do not exist in the JSON content.
Special case: prefix syntax The ?. operator makes it possible to use a WLanguage function directly on a nullable type, without using a variable. Syntax: result = nullable_element?. WLanguageFunction(...) - If nullable_element is not Null, the function is called.
- If nullable_element is Null, the function is not called and the result is Null.
Example: chaine_complète_ou_Null is string ? = ...
dix_premiers_caractères_ou_Null is string ? = chaine_complète_ou_Null ?. Left s(10)
You can combine the ?. and ?? operators to get a simple result: dix_premiers_caractères is string = chaine_complète_ou_Null ?. Left s(10) ?? ""
Using null values in procedures Passing parameters by value Null or nullable values cannot be passed to a non-nullable parameter of a simple type, or to a non-typed non-nullable parameter. To pass Null or a nullable value to a parameter, use the following syntaxes:
PROCEDURE ProcedureWithANullableParameter(parameter?) PROCEDURE ProcedureWithANullableParameter(parameter is int?) Note: Implicit conversions between simple types apply between the corresponding nullable types. Return value Similarly to parameters, you must specify that the return type is nullable if a procedure is to return Null or a nullable value. The corresponding syntaxes are: PROCEDURE ProcedureReturnsANullableValue ():? PROCÉDURE ProcedureRenvoieUneValeurNullable (): integer? Operations available on variables that allow null values Arithmetic operations Arithmetic and string operations are not allowed if one of the operands is Null. Executing these operations triggers an exception.
A compilation error allows you to identify these cases. Arithmetic operations: | | - + (addition and concatenation)
- ++
- +=
- -
- --
- -=
| - *
- *=
- / (division and string concatenation for paths)
- /=
- %
- ^
|
Comparison operations The = (equal to) operator returns: - True if both values are Null;
- False if one value is Null and the other is not.
The <> (not equal to) operator returns: - True if one value is Null and the other is not;
- False if both values are Null.
Other comparisons raise an exception if one of the values is Null: Boolean operations Boolean operations have a particular behavior with nullable Boolean values. - PAS
- NOT Null = Null
- NOT False = True
- NOT True = False
- OR
| | | | | Null | False | True | Null | Null | Null | True | False | Null | False | True | True | True | True | True |
- ET
| | | | | Null | False | True | Null | Null | False | Null | False | False | False | False | True | Null | False | True |
Conditions In a condition (IF, WHILE, etc.), the Null value is equivalent to False.
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|