Recent

Author Topic: How to forward a Class in {$mode tp} in 2 different 'type's?  (Read 973 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1072
How to forward a Class in {$mode tp} in 2 different 'type's?
« on: January 12, 2026, 11:57:44 am »
The documentation says in https://www.freepascal.org/docs-html/current/ref/refse35.html
Quote
A class forward definition is simply the name of the class, with the keyword Class, as in the following example:
...
When using a class forward definition, the class must be defined in the same unit, in the same section (interface/implementation). It must not necessarily be defined in the same type section.

But the sentence I have marked green seems to be only valid in {$mode objfpc} and not in {$mode tp}

Code: Pascal  [Select][+][-]
  1. {$mode tp}{$H+}      {this gives Error: Type "Class1" is not completely defined}
  2. { $mode objfpc}{$H+} {this compiles}
  3. {$modeswitch class}  {allows classes in '$mode tp'}
  4.  
  5. type
  6.    Class1 = class; {forward declaration}
  7.  
  8.    Class2 = class
  9.      C1: Class1;
  10.    end;
  11.  
  12. {... a lot of code ...}
  13.  
  14. type // without this separate 'type' it would compile in {$mode tp}
  15.    Class1 = class
  16.      C2: Class2;
  17.    end;
  18.  
  19. begin
  20. end.

In {$mode tp} the compiler shows Error: Type "Class1" is not completely defined. If I would delete the 2nd 'type', it would work. But to move both declarations into the same 'type' I a) would have to rearrange a lot of Code and b) the readability / structure would become worse.

The documentation of {$mode tp} in https://www.freepascal.org/docs-html/current/prog/progse73.html says nothing about this restriction :-((

My question: is there a {$modeswitch xyz} which I can add in {$mode tp} to solve the problem? Or something else?
Thanks in advance.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12645
  • FPC developer.
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #1 on: January 12, 2026, 12:20:50 pm »
No. Turbo Pascal has no classes, so this is not supported behaviour IMHO

That forward class declarations must be resolved within a type block is normal.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #2 on: January 12, 2026, 12:58:31 pm »
IIRC forward object declarations are also not allowed in mode TP.
You can tinker with the modeswitches, but then you get kind of a bastard TP mode.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Hartmut

  • Hero Member
  • *****
  • Posts: 1072
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #3 on: January 12, 2026, 04:12:40 pm »
No. Turbo Pascal has no classes, so this is not supported behaviour IMHO
To support classes I had added {$modeswitch class}. I did expect, that this would enable classes completely, including forward class definitions in different 'type' sections.

Quote
That forward class declarations must be resolved within a type block is normal.
Did you see that the documentation (see link above) for this says:
Quote
... It must not necessarily be defined in the same type section.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #4 on: January 12, 2026, 06:05:21 pm »
But it must be defined in A type section. Not anywhere else.
Note even forward class declarations is fairly recent anyway.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Hartmut

  • Hero Member
  • *****
  • Posts: 1072
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #5 on: January 12, 2026, 07:04:01 pm »
But it must be defined in A type section. Not anywhere else.
As so often your post helps nobody - only you to increase your post counter.
Of course a class must be defined in a type section!
If you would have had a short look at my demo, you would have seen this.

Quote
Note even forward class declarations is fairly recent anyway.
That's wrong. In {$mode objfpc} forward class declarations work - also separated in 2 different type sections - even in my very oldest FPC which is 2.6.4 (!) from March 2014. Thats not "fairly recent".

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #6 on: January 12, 2026, 10:03:57 pm »
Note even forward class declarations is fairly recent anyway.

Huh? If you consider 20 years fairly recent... :o

Bart

  • Hero Member
  • *****
  • Posts: 5675
    • Bart en Mariska's Webstek
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #7 on: January 12, 2026, 10:35:22 pm »
Huh? If you consider 20 years fairly recent... :o

Well, that just feels like yesterday to me  O:-)

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #8 on: January 13, 2026, 12:02:46 pm »
Yes, I was not clear. I meant to explain that the following is illegal in mode TP or TP itself:
Code: Pascal  [Select][+][-]
  1. {%FAIL%}
  2. {$ifdef fpc}{$mode tp}{$endif}
  3. { pure TP }
  4. type
  5.   TMyObject = object;  // try forward
  6.   TmyObject2 = object
  7.     obj:TMyObject;  // try use it
  8.   end;
  9.  
  10.   TMyObject = object  // try implementation of forward
  11.   end;
  12. begin
  13. end.
As explained, TP has no concept of class. Adding {$modeswitch class} mutilates the intend.
And this is legal code:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. type
  3.   TMyObject = class;
  4.   TmyObject2 = class
  5.     obj:TMyObject;
  6.   end;
  7.  
  8.   TMyObject = class
  9.   end;
  10. begin
  11. end.
Just because of the mode and class concept.
I might add that Harmut's code should work if the modeswitch is in the right place, since this compiles:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode tp}{$modeswitch class}{$endif}
  2. type
  3.   TMyObject = class;
  4.   TmyObject2 = class
  5.     obj:TMyObject;
  6.   end;
  7.  
  8.   TMyObject = class
  9.   end;
  10. begin
  11. end.
If it is sane, though, I have no opinion, since I also sometimes create bastard modes.
Modeswitches must be added AFTER choosing the default mode, otherwise they get reset.

Since his snippet has the right order I am a bit bemused Hartmut did not get it to work.


« Last Edit: January 13, 2026, 12:47:30 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

zamtmn

  • Hero Member
  • *****
  • Posts: 679
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #9 on: January 13, 2026, 01:12:55 pm »
TP allow only pointer forward declaration

Hartmut

  • Hero Member
  • *****
  • Posts: 1072
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #10 on: January 13, 2026, 07:41:31 pm »
Since his snippet has the right order I am a bit bemused Hartmut did not get it to work.
Sorry, again you did not understand the question.

In {$mode tp} the compiler shows Error: Type "Class1" is not completely defined. If I would delete the 2nd 'type', it would work.
As I wrote in my 1st post, class forward definitions do work in {$mode tp}.
But in {$mode tp} they must be defined in the same type section.
While in {$mode objfpc} they must not be defined in the same type section.
The documentation (see link above) for this says:
Quote
... It must not necessarily be defined in the same type section.
without including a restriction in {$mode tp}.



TP allow only pointer forward declaration
Sorry, this is wrong, as I wrote in my 1st post and as Thaddy tested in his 3rd example.

Again my question:
is there a {$modeswitch xyz} (or a Compiler switch? or something else?) which I can add in {$mode tp} to allow class forward definitions in {$mode tp} not only in the same type section, but in different type sections, as in {$mode objfpc} is possible and as the documentation says?

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #11 on: January 15, 2026, 09:21:47 pm »
Again my question:
is there a {$modeswitch xyz} (or a Compiler switch? or something else?) which I can add in {$mode tp} to allow class forward definitions in {$mode tp} not only in the same type section, but in different type sections, as in {$mode objfpc} is possible and as the documentation says?

No, there is no modeswitch for that, because that is a speciality of the two FPC modes.

Hartmut

  • Hero Member
  • *****
  • Posts: 1072
Re: How to forward a Class in {$mode tp} in 2 different 'type's?
« Reply #12 on: January 16, 2026, 05:21:35 pm »
Thank you PascalDragon for that clear answer.

 

TinyPortal © 2005-2018