Variant

The variant “magic” datatype can store most base types.

var v: Variant;
begin
  WriteLn('Should be unassigned: ', not v.IsAssigned());
  WriteLn();

  v := 'I am a string';
  Writeln('Now should *not* be unassigned: ', v.IsAssigned());
  WriteLn('And should be string:');
  WriteLn(v.VarType, ' -> ', v);
  WriteLn();

  v := Int64(123);
  WriteLn('Now should be Int64:');
  WriteLn(v.VarType, ' -> ', v);
  WriteLn();

  v := 0.123456;
  WriteLn('Now should be Double:');
  WriteLn(v.VarType, ' -> ', v);
end;

Note:: If curious to how the Variant datatype works, internally it’s a record:

// pseudo code
type
  InternalVariantData = record
    VarType: EVariantType;
    Value: array[0..SizeOf(LargestDataTypeVariantCanStore)] of Byte;
  end;

Variant.VarType

function Variant.VarType: EVariantVarType;

Returns the variants var type.

Example::

if (v.VarType = EVariantVarType.Int32) then WriteLn(‘Variant contains a Int32’);


Variant.IsNumeric

function Variant.IsNumeric: Boolean;

Is integer or float?


Variant.IsString

function Variant.IsString: Boolean;


Variant.IsInteger

function Variant.IsInteger: Boolean;


Variant.IsFloat

function Variant.IsFloat: Boolean;


Variant.IsBoolean

function Variant.IsBoolean: Boolean;


Variant.IsVariant

function Variant.IsVariant: Boolean;

The variant holds another variant!


Variant.IsAssigned

function Variant.IsAssigned: Boolean;

Example:

  if v.IsAssigned() then
    WriteLn('Variant HAS been assigned to')
  else
    WriteLn('The variant has NOT been assigned to');

Variant.IsNull

function Variant.IsNull: Boolean;


Variant.NULL

function Variant.NULL: Variant; static;

Static method that returns a null variant variable.

Example:

  v := Variant.NULL;