Nullable Types in Expressions

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.



Delphi Prism provides the capability to use Nullable Types in standard arithmetic and boolean expressions.

To explain how nullable types work in expressions, we assume the following (overly simplified) grammar:

<expr> ::=
    <expr> <binaryop> <expr>
  | <unaryop> <expr>
  | <value>

Then we have a tree structure of expressions with <value> expressions forming the leaf nodes. These <value> nodes have a known type. To determine the type of the root <expr> the compiler starts at the leaf nodes (which have know types) and works its way towards the root, determining the type of <expr> nodes based on the operator and operand types.

Determining Type

To determine the type of an <expr>, the following rules are applied:

For example:

Int32 + nullable Int32   ''=>'' nullable Int32

This applies to all operators with only a single important exception:

Examples

Int32 = nullable Int32   ''=>'' boolean
Int32 < nullable Int32   ''=>'' nullable boolean

Evaluating

When evaluating expressions, the result will be nil/null, if one or both of the operands is nil; otherwise the result is the result of applying the operator to the values of the operands.

Examples (assuming right-hand operator is a nullable Int32 type):

35 + nil = nil
35 + 5   = 40;

Equality

The following rules apply to the equality (= and <>) operators:

Non-Equality

Booleans

Truth table for the NOT boolean operator

NOT (True) is False, 
NOT (False) is True, 
NOT (Unknown) is Unknown.

Truth table for the AND boolean operator

AND     True    False Unknown
True    True    False Unknown
False   False   False False
Unknown Unknown False Unknown

Truth table for the OR boolean operator

OR      True False   Unknown
True    True True    True
False   True False   Unknown
Unknown True Unknown Unknown

Truth table for the XOR boolean operator

There is no XOR operator in SQL, But "A xor B" can be expressed as "not (A and B) and (A or B)", based on that and the truth tables above it's possible to derive the truth table for XOR:

XOR     TRUE    FALSE   UNKNOWN
True    False   True    Unknown
False   True    False   Unknown
Unknown Unknown Unknown Unknown

Boolean Short-Circuit

Boolean short-circuit evaluation is possible for the following operators if the first operand has a specific value:

(The introduction of boolean short-circuit evaluation for XOR is new)


See Also


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox