Nullable Value Support

Code Effects supports nullable value types throughout rule authoring and evaluation. Every supported type has a nullable form: bool?, the nullable numeric types, DateTime?, DateOnly?, DateTimeOffset?, TimeOnly?, TimeSpan?, and nullable enumerations. Nullable properties, fields, collection items, method parameters, and method return values take part in rules in a natural and predictable way, so rule authors can think about business logic and not about the declarations of the data model.

NOTE: The rules in this article apply in addition to the automatic numeric type conversion of Code Effects. The engine converts compatible numeric types during evaluation, and the Rule Editor uses the same logic to build its menus.

A reference in this article to decimal or decimal? is shorthand for any numeric type that converts to it. A parameter declared as decimal can also accept a short, int, long, float, or double, as long as the nullable restrictions below are met, and a decimal? parameter accepts both nullable and non-nullable numbers that convert to decimal?.

The Operators of a Nullable

A nullable member has two operators that a non-nullable does not, has no value and has any value. They are in the same menu with the other operators of the member's type:

The operators of a nullable number

They work the same way for every type, including nullable enumerations:

The operators of a nullable enumeration

Fields, Properties, and Method Return Values

Nullable and non-nullable value types can be compared directly in a rule condition without any restrictions. When one operand is non-nullable and the other is nullable, the engine converts the non-nullable value to its nullable equivalent and compares them with the operator of the condition.

The following comparisons are supported:

  • int compared with int?
  • decimal? compared with decimal
  • DateTime compared with DateTime?
  • A nullable method return value compared with a non-nullable field
  • A non-nullable method return value compared with a nullable field

This is true for fields, properties, and the return values of in-rule methods, so a rule author does not have to tell nullable and non-nullable members apart.

Rule Editor Behavior

The Rule Editor offers nullable and non-nullable members together. After the author selects a non-nullable int field, the value menu has all the compatible integer values:

  • Non-nullable integer fields and properties (int)
  • Nullable integer fields and properties (int?)
  • In-rule methods that return int
  • In-rule methods that return int?

It works the same way when the author starts with a nullable member.

Method Parameters

The parameters of in-rule methods and rule actions have stricter rules.

  • Nullable Parameters

    A parameter declared as a nullable value type accepts nullable and non-nullable values. A decimal? parameter accepts the values of decimal and decimal? fields and properties.

  • Non-Nullable Parameters

    A parameter declared as a non-nullable value type accepts only non-nullable values. A decimal parameter accepts the values of decimal sources only, never of decimal?.

Nullable parameters are permissive and non-nullable parameters are strict. The rules are the same for in-rule methods and rule actions. Strings and collections are exempt from them, because a reference type can be null anyway.

When a rule breaks them, the editor marks the value element invalid and shows the message "This value can be empty (null), but what it is given to cannot be". Its identifier is v163, and you can translate it with the Help XML.

Value Setters in Execution Rules

Execution type rules apply the same restrictions to the values assigned after Then.

  • Assigning to a Nullable

    A nullable field accepts values from nullable and non-nullable sources. A decimal? field can be assigned from decimal and decimal?.

  • Assigning to a Non-Nullable

    A non-nullable field accepts values from non-nullable sources only. A decimal field cannot be assigned a value from decimal?.

Collections and Arrays of Value Types

Collections and arrays of value types follow the same rules as parameters and setters.

  • Collections of Nullable Items

    A List<decimal?> or an array of nullable items accepts nullable and non-nullable values. It can be checked against decimal and decimal? values. A collection of nullable enumerations, List<PatientState?>, is supported as well.

  • Collections of Non-Nullable Items

    A List<decimal> accepts non-nullable values only. This is therefore not supported:

      Check if List<decimal> contains decimal?
    

    but this is:

      Check whether List<decimal?> contains decimal
    

    Arrays follow the same rules.

See Collection Data Types.

Nullable Enumerations

Nullable enumerations are supported in v7. A field of the type PatientState? offers the members of the enumeration, the operators is, is not, is in, is not in, and, like every nullable, has no value and has any value. Versions of Code Effects before v7 ignored nullable enumerations when they read a source object; if your source objects hide one behind a non-nullable property, you can drop the workaround.

public enum PatientState { Arizona, California, Florida }

public class Patient
{
    [Field(DisplayName = "Previous State")]
    public PatientState? PreviousState { get; set; }
}

Adaptive Source Considerations

An Adaptive Source controls which fields, methods, and actions the editor receives with every menu request. The editor keeps a cache of the source elements it has received, to stay responsive. If the cache already holds enough compatible members, the editor applies its nullable logic to them and can show cached nullable and non-nullable members together, even when they were not part of the latest response.

Summary

  • Conditions compare nullable and non-nullable value types freely.
  • Every nullable has the operators has no value and has any value.
  • Nullable parameters accept nullable and non-nullable values, and non-nullable parameters accept non-nullable values only.
  • The setters of execution rules follow the rules of parameters.
  • Collections and arrays follow the same model.
  • Nullable enumerations are supported.

Ask on Stack Overflow - Yes, it's still active
Ask in GitHub Discussions

p102

l097 --

l102

p101

×