Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi variants and TValues

Tags:

delphi

I know that Variants are slow when I convert data types, even when I simply add two variant values holding integers. I have discovered also another similar thing that looks variant-like called TValue.

What's the difference between them?

I was considering to make a classic speed test using the TStopWatch, but I don't think that it is enough to understand the differences

like image 709
Raffaele Rossi Avatar asked Sep 21 '16 19:09

Raffaele Rossi


2 Answers

TValue is not meant for converting data types but for transportation of values when using RTTI.

It only allows for data types to be converted that are directly assignment compatible (like for example Integer to Int64 or Byte to Integer). But unlike Variant it does not allow for example converting a string containing a number to be cast to Integer but raises an EInvalidCast exception.

like image 126
Stefan Glienke Avatar answered Sep 22 '22 17:09

Stefan Glienke


TValue is not Variant-like. It will not perform data conversions like Variant does. If you ask TValue to return a different type than it is currently holding, and the held type is not assignment-compatible with the requested type, (ie it would not normally implicitly convert in normal language syntax), then TValue will raise an EInvalidCast exception.

like image 35
Remy Lebeau Avatar answered Sep 22 '22 17:09

Remy Lebeau