Nullable Types in Expressions

From RemObjects Software

Jump to: navigation, search

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



Oxygene 3.0 introduces 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:

  • the nullability characteristic of the operands is ignored, determining the result type in the usual way
  • if any of the operands is nullable, the result type nullable.

For example:

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

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

  • equality comparisons (= and <>) always result in a not-nullable boolean, to preserve the established logic of comparing referenced based values. Other comparison operators (< <= > >=) will produce a nullable boolean as result if one of the operands is nullable.

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:

  • nil = nil => true
  • nil = non-nil => false
  • non-nil = nil => false
  • non-nil = non-nil => compare value

Non-Equality

  • nil <> nil => false
  • nil <> non-nil => true
  • non-nil <> nil => true
  • non-nil <> non-nil => compare value

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:

  • AND - False
  • OR - True
  • XOR - Unknown

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


See Also

Product: RemObjects Oxygene (formerly known as Chrome)
Current version: 3.0 Previous Versions: 'Joyride' (2.0), 'Floorshow' (1.5), 'Adrenochrome' (1.0)

GlossaryKeywordsLanguage FeaturesPlatform FeaturesSamplesArticlesHow ToIssues

Personal tools