value, as, and to [Variant class]

There are several variations of accessors to Variant values used for different purposes:

Accessor Description
value() Returns a reference to the currently held value. Requires it be of the type T otherwise would cause undefined behavior.
as() or asXXX() converts the underlying value of the variant to T in place, modifying the variant, and returns a reference to it. Safe to call if the types don't match but requires T to be one of the supported types, otherwise it will cause a compilation error.
to() or toXXX() converts the underlying value to T without modifying the variant and returns the result by value. Safe to call if the types don't match, T can be arbitrary.
  • When reading complex values like string, array, and map, using the value operator uses a reference (not a copy) so it's more efficient.
  • When assigning a value to a Variant, the asXXX() methods can be set the variant to be that type. Useful when building the data structure to set ledger values.
  • When reading values from a Variant, the toXXX() methods can be used to copy the data convert it as necessary, without modifying the original.