[fpc-pascal] more Variant thoughts (was: Variant record types in Sockates unit)
Michael Van Canneyt
michael at freepascal.org
Wed Oct 4 10:38:36 CEST 2006
On Wed, 4 Oct 2006, Alexander Todorov wrote:
> I made the simple test below:
>
> program project1;
>
> {$ifdef fpc}
> {$mode objfpc}{$H+}
> {$endif}
>
> {$apptype console}
>
> type
>
> TTagVariant = record
> AlwaysThere : Integer;
> case Flag : Boolean of
> false: (false_1, false_2 : Byte);
> true : (true_1: Word);
> end;
>
> var V : TTagVariant;
>
> begin
> V.AlwaysThere := 1;
>
> V.Flag := false;
> V.true_1 := 200; // should compiler raise a run time error here
> V.false_1 := 100;
> writeln('V.Flag=', V.Flag, ', V.false_1=', V.false_1, ',
> V.true_1=', V.true_1);
>
> V.Flag := true;
> V.true_1 := 200;
> V.false_1 := 100;// should compiler raise a run time error here
> writeln('V.Flag=', V.Flag, ', V.false_1=', V.false_1, ',
> V.true_1=', V.true_1);
>
> writeln('Addresses: ', 'V.false_1=', Integer(@V.false_1), ',
> V.true_1=', Integer(@V.true_1));
> end.
>
> The results are:
> V.Flag=FALSE, V.false_1=100, V.true_1=100
> V.Flag=TRUE, V.false_1=100, V.true_1=100
> Addresses: V.false_1=134632390, V.true_1=134632390
>
> What I am thinking about is:
> 1) If the Flag field is set to true and the programmer uses field from
> the variant part which is declared in the false section, should there
> be a run time error or not?
No. No checking is performed on this.
> Although it will be slowly should the compiler check V.Flag and
> consider the other fields undefined? Something like "Inaccessible
> record field used" error?
No.
The 'Flag' field is purely informational and has no restrictive meaning.
It's a hint to the programmer, but nothing more.
This is how pascal is defined.
Michael.
More information about the fpc-pascal
mailing list