[fpc-pascal] Checking assignment compatibility

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Mon Jan 6 11:23:16 CET 2014


Jonas Maebe wrote:

>>>>> Assert(TypeOf(qt) = TypeOf(result));
>>>> This is always true. Everything descends from TOBject.
>>>> But assuming the result is not TObject, it would be
>>>> Assert((qt=Nil) or (qt.Inheritsfrom(TheResultClass))
>>>
>>> So again skipping the nil check for simplicity, this compiles but are 
>>> the semantics right?
>>>
>>> Assert(qt.ClassType.InheritsFrom(result.ClassType));
>>
>> The classtype is not needed.
> 
> The first one, at least.
> 
>> The semantics are not right IMHO, but I'd have to see the original C 
>> stuff to be able to say with certainty.
> 
> Regardless of what the original C stuff was, the above is wrong because 
> "result" is uninitialised. Inside the function you cannot know the type 
> of whatever the result will be assigned to on the caller side. Maybe 
> instead of "result" it should be "qf".

I see. So I could possibly do it with a third parameter provided that 
this was initialised to a dummy object, but otherwise the only possible 
check is that the two source objects are of the same type. In practice 
it's far better done with an inline  if  statement.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list