Nullable Types

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 native support for nullable types. It's possible to write

var i: nullable Integer;

which will be emitted as a System.Nullable<Integer>, but any member access call on it will act on the integer instead of the Nullable<Integer> type, so any member on Integer can be called directly on the nullable integer without having to use the .Value call first. This makes using nullable types much more transparent and requires a lot less code to write for working with nullable types.

We've also intro a new system method valueOrDefault that returns the default value of the sub-type of the nullable, or a value of your choice.

class method ConsoleApp.Main;
 var 
   lInt: nullable Integer;
   lVal: Integer;
 begin
   lInt := 15;
   lVal := lInt.CompareTo(12);
   Console.WriteLine('CompareTo returned: '+ lVal.ToString); // will write a number > 0
    
   lInt := nil;
    
   lVal := valueOrDefault(lInt, -1); 
   Console.WriteLine(lVal); // Will write -1 as lInt is nil
 end;

The Colon or "Non-nil Access" Operator

The language provides a special operator type, the Colon Operator, or (:). This new operator acts like the regular member access operator (.) but makes sure that the call does not happen when the value is nil, instead the result of what would have been the call is nil.

Expressions

Delphi Prism support for nullable types allows them to be seamlessly used within expressions. See Nullable Types in Expressions for more details on this feature.


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox