[fpc-devel] mantis 0038496 custom variants and documentation

Sven Barth pascaldragon at googlemail.com
Sun May 30 13:02:19 CEST 2021


Am 30.05.2021 um 00:53 schrieb Marco van de Voort via fpc-devel:
> Before the 3.2.2 release I looked into mantis 0038496 and now I come 
> back to it.
>
> I noticed that custom variants are completely undocumented, is this 
> know (IOW should I file a bug?). What I wanted to look up are the 
> rules for implementing binaryop, since that is where the problem is.
>
> The bug is for the following code:
>
> {$ifdef fpc}
>  {$mode delphi}
> {$else}
>  {$apptype console}
> {$endif}
>
> uses variants,fmtbcd;
> var
>   fBCD1: TBcd;
>   V1, V2, V3:Variant;
>   L1: Integer;
> begin
>   L1:=123;
>   fBCD1:=1234.345;
>
>   V1:=123;
>   V2:=VarFmtBCDCreate(fBCD1);
>   V3:=V1 + V2;
>   writeln(v3);
>   readln;
> end.
>
> The addition finally ends up in  TFMTBcdFactory.BinaryOp which is 
> declared as follows
>
> procedure TFMTBcdFactory.BinaryOp(var Left: TVarData; const Right: 
> TVarData; const Operation: TVarOp);
>
> i.e.  not a 3 operand action, but a two operand.  This probably means 
> that in the case of v3:=v1+v2 this is encoded as v1:=v1,v2,add, 
> modifying V1.
>
> The fmtbcd binaryop code however says that the left type (integer)  is 
> not either double or a a FMTBCD custom variant and thus throws an 
> exception.
>
> I think it is allowed to change the result type of the variant passed 
> in binaryop(), but am not 100% sure, and also not sure if there are 
> other things to keep an eye for (e.g. finalization of the existing 
> value).
>
> Does anybody know open source custom variant examples other than fmtbcd ?

The only one I'm aware of that makes use of BinaryOp is Python4Delphi ( 
https://github.com/pyscripter/python4delphi/blob/master/Source/VarPyth.pas 
). mORMot derives its TSynInvokeableVariantType and thus its TDocVariant 
from TInvokableVariantType, but does not override the operator methods.

Regards,
Sven


More information about the fpc-devel mailing list