Lazarus

Free Pascal => General => Topic started by: paulvanderlinden on July 18, 2022, 01:50:37 pm

Title: Comparing pointers to base objects
Post by: paulvanderlinden on July 18, 2022, 01:50:37 pm
Hi,

First off, not sure if this is the correct subforum, please inform me if not.
In delphi, this was perfectly valid code:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4. Pbbase = ^Obbase;
  5. Obbase = object
  6. end;
  7.  
  8. Pbderived = ^Obderived;
  9. Obderived = object(Obbase)
  10. end;
  11.  
  12. var base:PbBase;
  13.       Derived:Pbderived;
  14. begin
  15.   new(Base);
  16.   new(Derived);
  17.   if Base=Derived then
  18.     writeln('yes');
  19. end.

However compiling in lazarus/freepascal gives me an error about incompatible types Pbbase and Pbderived.
I know I could put a cast around it (
Code: Pascal  [Select][+][-]
  1. if Base=PbBase(Derived) then
but that is a bit to non-restrictive, if f.e. I decouple base from derived the cast would still compile....

Paul
Title: Re: Comparing pointers to base objects
Post by: marcov on July 18, 2022, 02:00:58 pm
It seems that Delphi does some IS like functionality here. Possibly an extension on Turbo Pascal ?!)?!

You can try to file a bug, but as it is very legacy functionality, it might lay a while.
Title: Re: Comparing pointers to base objects
Post by: paulvanderlinden on July 18, 2022, 02:18:36 pm
Ok, thanks.
So definitely not a setting I missed.

Turned out, assigning the derived to a tempvar of type base does the trick too.
Not the cleanest solution, but at least it's the same functionality. and with a bit of luck, this will be optimized out by the compiler anyway
Title: Re: Comparing pointers to base objects
Post by: ASerge on July 20, 2022, 09:59:38 pm
It seems that Delphi does some IS like functionality here. Possibly an extension on Turbo Pascal ?!)?!
From Turbo Pascal 5.5 docs:
Quote
Type compatibility also operates between pointers to object types, under the same general rules as with instances of object types: Pointers to descendants can be assigned to pointers to ancestors.
The Delphi compiler still supports this.
Title: Re: Comparing pointers to base objects
Post by: marcov on July 21, 2022, 09:19:30 am
It seems that Delphi does some IS like functionality here. Possibly an extension on Turbo Pascal ?!)?!
From Turbo Pascal 5.5 docs:
Quote
Type compatibility also operates between pointers to object types, under the same general rules as with instances of object types: Pointers to descendants can be assigned to pointers to ancestors.
The Delphi compiler still supports this.

This is not an assignment, but a comparison. But it would be nice to know what TP/BP 7 (our reference version) does with this code.
Title: Re: Comparing pointers to base objects
Post by: Thaddy on July 24, 2022, 04:52:54 pm
@Marco
TP does a deep comparison, so a full run over both objects. as opposed to a shallow comparison, where just the pointers and size need to be equal.
Note that deep comparisons are rather slow, especially in a loop. Delphi does still a deep comparison on objects. FPC doesn't.
So it is a - afaik not documented! - compatibility issue. Either that, or if this was not intentional it is a bug.
TinyPortal © 2005-2018