Recent

Author Topic: Idea to solve a circular reference issue with classes.  (Read 2764 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12857
  • FPC developer.
Re: Idea to solve a circular reference issue with classes.
« Reply #15 on: March 30, 2026, 01:02:34 pm »
    2.  IDEA 1:
           Forwarding a Class;
           Class Name = Class IS UnitNameThatHostIT.CLASSNAME;
           Maybe the compiler can just look there once for the Class definition?

In a circular dependency relation, the UnitNameThatHostIt is not be compiled yet, so you can't reference it.

     3. IDEA 2:
            Have the compiler look at the Uses List in the Implementation section and use that reference if one isn't declared in the interface section with maybe a special indicator or HINT ?

Declare before use is pretty big in Pascal, and such reference is a "use".

Interface and implementation and order of compilation in general is quite rigidly defined in a single pass compiler. You would need to change the whole compiler and language to a multi pass model to realistically do something so direct. 

There are other workarounds.

Factor one of the classes to a base class in a different unit, with virtual methods if needed. Yes, that is not you need to typecast if you need derivative properties.  Or if typecasting gets too much, use a type helper with an inline method that does the casts etc.


creaothceann

  • Sr. Member
  • ****
  • Posts: 361
Re: Idea to solve a circular reference issue with classes.
« Reply #16 on: March 30, 2026, 06:35:59 pm »
Maybe I'm not understanding the problem correctly, but again, what exactly is the problem with putting these classes into the same unit?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12857
  • FPC developer.
Re: Idea to solve a circular reference issue with classes.
« Reply #17 on: March 30, 2026, 06:52:38 pm »
Maybe I'm not understanding the problem correctly, but again, what exactly is the problem with putting these classes into the same unit?

That's pretty much the default solution yes. I assumed that was not possible.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6395
  • Compiler Developer
Re: Idea to solve a circular reference issue with classes.
« Reply #18 on: March 30, 2026, 08:33:15 pm »
IDEA 1: [...]
IDEA 2: [...]

IDEA3:
Allow an external declaration for the class. Then the class could only be used to define member variables (which normally is enough) and not to derive a class etc.
Code: Pascal  [Select][+][-]
  1. Type
  2.   TFoo = class; external;
(hypothetic code)

EDIT: No idea how to implement that. I guess at some point the unit where the class is expected has to be specified.

I had tried that some years ago considering that the JVM target supports that for external classes, but it blew up in various corner cases and I scraped the idea for the time being.

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: Idea to solve a circular reference issue with classes.
« Reply #19 on: March 31, 2026, 12:19:48 pm »
Maybe we can try again?
Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: Idea to solve a circular reference issue with classes.
« Reply #20 on: March 31, 2026, 01:51:50 pm »
With an instantiated class that works.. With the class type it doesn't.
But look at my last code example: that does work, but not without softcasts.

If you can provide me with some external code that you want to address, I would be happy to explore other solutions than interface or old school.
objects are fine constructs. You can even initialize them with constructors.

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: Idea to solve a circular reference issue with classes.
« Reply #21 on: April 12, 2026, 10:55:07 am »
So that's it, fight with a stubborn compiler.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018