Recent

Author Topic: Suggestion - implement FORWARD keyword for records.  (Read 2636 times)

ad1mt

  • Sr. Member
  • ****
  • Posts: 327
    • Mark Taylor's Home Page
Suggestion - implement FORWARD keyword for records.
« on: December 18, 2023, 07:09:17 pm »
Here's something I needed while working on a library...
implement FORWARD keyword for records...  :o
I know its non-standard, but it prevented me from doing something that would have been really useful.

cdbc

  • Hero Member
  • *****
  • Posts: 1655
    • http://www.cdbc.dk
Re: Suggestion - implement FORWARD keyword for records.
« Reply #1 on: December 18, 2023, 07:14:36 pm »
Hi
PRecord = ^TRecord;
Doesn't work for you???
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: Suggestion - implement FORWARD keyword for records.
« Reply #2 on: December 19, 2023, 01:22:35 pm »
What would be very useful... Example plz.
If I smell bad code it usually is bad code and that includes my own code.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11938
  • FPC developer.
Re: Suggestion - implement FORWARD keyword for records.
« Reply #3 on: December 19, 2023, 01:46:47 pm »
Currently forward declarations for storage types are only for types from which the storage size is known from the forward declaration.

i.e. a pointer (in the disguise of a class instance ref). This is not possible for a Record, since to embed that in another class of structure the size is required.

To fix that would require a radical different multipass parser model, which is beyond the scope of this project atm.

Similarly forward procedure declarations also have the base declaration to call it, for exactly the same reasons.
« Last Edit: December 19, 2023, 06:20:07 pm by marcov »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: Suggestion - implement FORWARD keyword for records.
« Reply #4 on: December 19, 2023, 10:01:46 pm »
Here's something I needed while working on a library...
implement FORWARD keyword for records...  :o
I know its non-standard, but it prevented me from doing something that would have been really useful.

And that is something that will not be changed. The compiler must know the size of a record to be able to embed it somewhere else (without using a Pointer) and both Pascal and FPC are designed as a single pass language (in case of FPC as far as the parser is concerned).

BeniBela

  • Hero Member
  • *****
  • Posts: 920
    • homepage
Re: Suggestion - implement FORWARD keyword for records.
« Reply #5 on: December 20, 2023, 12:20:07 am »
And that is something that will not be changed. The compiler must know the size of a record to be able to embed it somewhere else (without using a Pointer) and both Pascal and FPC are designed as a single pass language (in case of FPC as far as the parser is concerned).

It does not need to change. The compiler could just support forward declarations without embedding.

And if the user tries to use the record in a field declaration, it gives an error "embedding not supported for forward types"



Such forward declarations are extremely useful. Then we could compile this:

Code: Pascal  [Select][+][-]
  1.  
  2. type
  3. //TB = record; forward;
  4.  
  5. TA = record
  6.   function equals(const b: TB): boolean;
  7. end;
  8. TB = record
  9.   function equals(const a: TA): boolean;
  10. end;
  11.  
  12.  

440bx

  • Hero Member
  • *****
  • Posts: 4731
Re: Suggestion - implement FORWARD keyword for records.
« Reply #6 on: December 20, 2023, 12:30:59 am »
Such forward declarations are extremely useful. Then we could compile this:
I see a conceptual problem with that code.   Specifically, there should be only ONE "Equals" function that takes a TA and TB as parameters.  Not two Equals functions that take different parameters yet, from the looks of it, are supposed to do the same thing.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018