Adding some convinience functions to TNullable

Summary

Adding some convenience functionalities to TNullable to ease working with it. The new functions are:

  • ValueOr(const Fallback: T): Similar to ValueOrDefault but let's the user define the Default to be used
// old
if optVal.HasValue then
  val := optVal.Value
else
  val := 'Default';
// new
val := optVal.ValueOr('Default');
  • Unpack(out aDest: T): Boolean: Allows to perform the value check and extracting of the value in a single call. Best suited for functions returning TNullable, avoiding to introduce a temporary variable:
// old
optVal := GetValueOpt;
if optVal.HasValue then
  val := optVal.Value
else
  // fallback behavior to set val
// New:
if not GetValueOpt.unpack(val) then
  // fallback behavior
  • null value: A global null constant that can be assigned to any nullable
// Old
optVal := specialize TNullable<String>.Empty;
// New
optVal := null;
  • Overloading Not operator: As most often nullables are checked if they are empty, to do some default behavior, by overloading the not operator
// old
if Not A.HasValue then
// new
if Not A then

See the tnull.pp test for a small test case for each of the functions

Merge request reports

Loading