ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

New WINDEV, WEBDEV and WINDEV Mobile 2024 feature!
Help / Editors / Project editor / Project description
  • 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
  • ?? 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
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Support NULL values: Allowing nullable types
Overview
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:
  1. Open the project description window: on the "Project" tab, in the "Project" group, click "Description".
  2. In the "Compilation" tab, check "Allow nullable types".
  3. A message about the implications of enabling this option appears. Click "Enable".
  4. 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.
Remark: There are no changes to be made to complex types (object, array, etc.): complex types support null values.
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 UNICODE string?
    n1 is 8-byte int?
  • use the <nullable> extension attribute.
    <Variable name> is <Type> <nullable>
    For example:
    n is int <nullable>
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:
  • non-nullable simple types can be compared with the Null constant: the result is always False.
  • nullable simple types can be compared with the Null constant: the result is True if the value is Null, False otherwise.
  • complex types (object, array, etc.) already supported Null values in previous versions: they can always be compared with Null values.
Remarks:
  • HFSQL item: If the data file and the item support Null values, it is now possible to determine if an HFSQL item is Null by directly comparing it with the Null constant. Example:
    IF Queue.ItemWithNullValue = Null THEN
    	// Process ...
    END
  • Edit control: If "Return NULL if empty" is checked for the control, it is possible to determine 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 = Queue.NullableItem
or
Left(Queue.NullableItem)

You need to specify how Null values should be handled, directly in the line of code. To do this, WLanguage provides several operators for accessing elements that allow nullable values: ?!, ??, ??*, ??= 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.
Syntax:
nullable_element?!
Example:
IF Queue.ItemWithNull <> Null THEN
	s is string = Queue.ItemWithNull?!
END
Reminder: If the code is wrong and the value is set to Null at runtime, an exception from the WLanguage security mechanism will occur.
Remark: In many cases, the?! operator is automatically added to make coding easier:
  • to elements that cannot be determined at compile time: query results, indirection, etc.
  • in certain conditional syntaxes. For example:
    IF Queue.ItemWithNull <> Null THEN
    	s is string = Queue.ItemWithNull	//?! operator automatically added
    END
Caution: even if the!? operator is automatically added, you need to specify how the Null value should be handled. Otherwise, an exception will be thrown at runtime.

?? 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:
// The string will contain "N/A" if the item is Null
s is string = Queue.ItemWithNull?? "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.
Syntax:
nullable_expression??*
Example:
// The string will be empty if the item is Null
s is string = Queue.ItemWithNull??*

??= 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:
nullable_value is string?
nullable_value = ProcedureToBeExecuted()
nullable_value??= "N/A"
This is more concise than the code below:
nullable_value is string?
nullable_value = ProcedureToBeExecuted()
IF nullable_value = Null THEN
	nullable_value = "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.
Syntax:
nullable_element??=*
Example:
nullable_value is string?
	nullable_value = ProcedureToBeExecuted()
	nullable_value??=*
This is more concise than the code below:
nullable_value is string?
nullable_value = ProcedureToBeExecuted()
IF nullable_Value = Null THEN
	nullable_value??*
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.
The TypeVar and DataType functions return the base type.
Starting with version 2024 Update 2, you can use TypeIsNullable to determine if a type is nullable.
Note: Many WLanguage functions support 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:
full_string_or_Null is string? = ...
first_ten_characters_or_Null is string? = full_string_or_Null?. Left s(10)
You can combine the ?. and ?? operators to get a simple result:
first_ten_characters is string = full_string_or_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?)
Remark: 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 ():?
PROCEDURE ProcedureReturnsANullableValue (): int?
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.
  • NOT
    • NOT Null = Null
    • NOT False = True
    • NOT True = False
  • OR
    NullFalseTrue
    NullNullNullTrue
    FalseNullFalseTrue
    TrueTrueTrueTrue
  • AND
    NullFalseTrue
    NullNullFalseNull
    FalseFalseFalseFalse
    TrueNullFalseTrue

Conditions

In a condition (IF, WHILE, etc.), the Null value is equivalent to False.
Minimum version required
  • Version 2024
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 03/22/2024

Send a report | Local help