[fpc-devel] About typecasts and the documentation

Martin Frb lazarus at mfriebe.de
Sat Feb 8 23:14:47 CET 2014


On 08/02/2014 21:59, Hans-Peter Diettrich wrote:
>
> Typecasts can be further restricted to *compatible* types. Here 
> numeric types seem to be compatible with other numeric types, but not 
> with structured types (records...).
Well than that needs to be added to the docs.  I have no problems with 
it ont working, I do not need it. What I do is, I do need to know what 
works, and how. And I need to know the rule behind.

Besides record and number are not incompatible: 
TRecordType(IntVariableOfSameSize) does work.

> It's not clear to me why a TStrings.Objects[i]:TObject can be 
> compatible with e.g. integer:
>   MyStringList.AddObject('1',TObject(1));
> This may be due to some underlying implementation detail, where the 
> list (array) contains pointers instead of objects, and these pointers 
> then are compatible with numbers.
Because internally the TObject "is a pointer" . (well that is not 
correctly worded..., but a variable of type TObject internally holds a 
pointer)

>
>> 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.
>
> Obviously the types (record and numeric) are considered incompatible 
> by the compiler.

Only if the number is a constant.

But a practical example:

type TFoo = packed record hi,lo: Byte; end;
var a: word;

if TFoo(a).hi = 1 then ; // that works
if TFoo($0101).hi = 1 then; // that does not




More information about the fpc-devel mailing list