[fpc-devel] About typecasts and the documentation
Martin Frb
lazarus at mfriebe.de
Sat Feb 8 21:34:36 CET 2014
On 08/02/2014 19:25, Martin Frb wrote:
>
> In the below (first) example "longint(1)" and TFoo have the same size.
....
> Yet in the first example they fail. This is not covered by the
> documentation.
> The doc even gives examples involving typecasting constant values.
>> Byte(’A’)
> given that the doc then goes on, and gives
>> Integer(’A’);
> that worked via the ordinal value (size does not match), it gives the
> impression that the first example was working under the rule of "same
> size".
>
> But then the doc does not give any clue why same size can not be cast
> to a record.
Even stranger, changing the example and the error adapts.
project1.lpr(10,8) Error: Illegal type conversion: "ShortInt" to "TFoo"
So fpc does actually resize the constant (even ignoring that it is
unsigned), and then complains....
(and yes SizeOf(TFoo) is 1)
{$A1}
program project1;
type
TFoo = record b:byte; end;
var
i: byte;
foo: TFoo;
begin
foo := TFoo(i);
foo := TFoo(byte(1));
foo := TFoo(1);
end.
>
>
> program project1;
> type
> TFoo = record b:longint; end;
> var
> i: longint;
> foo: TFoo;
> begin
> foo := TFoo(i);
> foo := TFoo(longint(1)); // project1.lpr(9,8) Error: Illegal type
> conversion: "LongInt" to "TFoo"
> foo := TFoo(1); // project1.lpr(10,8) Error: Illegal type conversion:
> "LongInt" to "TFoo"
> end.
>
> -------------------------------------
> program project1;
> type
> TFoo = record b:longint; end;
> var
> i: longint;
> foo: TFoo;
> begin
> foo := TFoo(i);
> writeln(SizeOf(TFoo));
> writeln(SizeOf(longint(1)));
> writeln(SizeOf(1));
> readln;
> end.
>
>
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
More information about the fpc-devel
mailing list