Menu

keywords-null

Will Pittenger

Overview

This can be used to store a null value into a variable or test that a variable is null. Also works on parameters and data members. Can be used with primitive types that were declared with the ref keyword or the ? operator and with all reference types that were not declared with the byvalue keyword. It also can be used to allow any enum type to be set to null. Variables set to null, regardless of type, should be considered as having no value. The default value for types that can be null is normally null.

Declaring compatible types

Types that are normally reference types

Simply declare these and be sure not to use the byvalue keyword.

(\const\ | {\var\ | [\const\]} [\readonly\]) /TypeDescriptor/ /identifier/ [\=\ /DefaultValue/]

For more on Type Descriptors, see Type descriptors.

Primitive types using the ref keyword

(\const\ | {\var\ | [\const\]} [\readonly\]) /PrimitiveTypeDescriptorWithRef/ /identifier/ [\=\ /DefaultValue/]

Primitive types using the ? operator

(\const\ | {\var\ | [\const\]} [\readonly\]) /PrimitiveTypeDescriptorWith?/ /identifier/ [\=\ /DefaultValue/]

enum types

Primitive-based enum types

Just list null in the list of valid values with no numeric value. Where it appears in the list isn't important. This will cause the compiler to assign a value like -1 to null. Then declare an instance of the type like normal.

\enum\ /TypeIdentifier/
    [extends /BaseTypeName/]
  [\null\]
  (/EntryIdentifier [\=\ /InternalValue/])...values

struct-based enum types

Again, null needs to be listed a value in the values list. Don't pass any parameters as you aren't calling your constructor. null values are never instances of your enum type. Once you declare the type, treat as you would any other struct-based enum type.

\enum\ \struct\ /TypeIdentifier/
    [extends /BaseTypeName/]
  \values\
    [\null\]
    (/EntryIdentifier/ [/ParameterList/])...values

Setting a instance of a compatible type to null

/SomeVariableThatCanBeNull/ \=\ \null\

/SomeVariableThatCanBeNull/ can be any l-value that can be null.

Testing for null

It's important to test variables of nullable types for null before you attempt to access their members, methods, properties, etc. Otherwise, ASIL will throw a NullValueError run-time error. The code before shows one way, but anything that can test a variable for null, like a while-type loop, will do the job. The sample below shows a check for null, but you might need to test for non-null. In that case, switch to the <> operator.

\if\ /SomeVariableThatMightBeNull/ \=\ \null\ \then\
  ' Do seomthing

Related

Wiki: Appendices-Terms-Type descriptors
Wiki: Derivatives-DASIL
Wiki: Enums
Wiki: Keywords
Wiki: keywords-byvalue
Wiki: keywords-enum
Wiki: keywords-params
Wiki: keywords-values
Wiki: operators-can-be-null