Nullable Types

From RemObjects Software

Jump to: navigation, search

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



Oxygene 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
   &nbsp;
   lInt := nil;
   &nbsp;
   lVal := valueOrDefault(lInt, -1);
   Console.WriteLine(lVal); // Will write -1 as lInt is nil
 end;

The Colon or "Non-nil Access" Operator

For 'Joyride', we introduced a new 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

Oxygene 3.0 extends support for nullable types by allowing them to be seamlessly used within expressions. See Nullable Types in Expressions for more details on this future feature.


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