Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-03

Linked list constant

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin Schreiber

Hi,
I need to build linked list constants, something like "
type
pbty = ^bty;

 aty = record
  b: pbty;
 end;
 paty = ^aty;
 bty = record
  a: paty;
 end;
const
 b0: bty = (a: @a0); //Error: Identifier not found "a0"
 a0: aty = (b: @b0);
"
Is it possible to make a forward declaration for a0?

Martin

Reply by email

leledumbo
to Martin Schreiber
Is it possible to make a forward declaration for a0?

No, forward type declaration is possible (with the restriction that the type must be based on pointer and the real declaration starts in the same type block), but forward variable declaration is not. You won't gain much benefit from doing the assignment at declaration than doing it at program start.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Linked-list-constant-tp5713665p5713678.html Sent from the Free Pascal - General mailing list .

Reply by email

Martin Schreiber
to leledumbo
leledumbo wrote 8 lines
> Is it possible to make a forward declaration for a0? No, forward type declaration is possible (with the restriction that the type must be based on pointer and the real declaration starts in the same type block), but forward variable declaration is not. You won't gain much benefit from doing the assignment at declaration than doing it at program start.

Really? No gain in the beloved readability? And it seems to me a "const" should not be changed by code. ;-)

Martin

Reply by email

leledumbo
to Martin Schreiber
Really? No gain in the beloved readability?

Not for me, I always initialize code in "executable" section, keeping the declaration section clean. That's what "readable" to me.

And it seems to me a "const" should not be changed by code. ;-)

Even if it's allowed, you're declaring typed constant, which is not a real constant and has the same effect as initialized variable (e.g. you can change the value later on). It only differs (from initialized var) when the assignment happens inside a routine.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Linked-list-constant-tp5713665p5713698.html Sent from the Free Pascal - General mailing list .

Reply by email

Martin Schreiber
to leledumbo
leledumbo wrote 12 lines
> Really? No gain in the beloved readability? Not for me, I always initialize code in "executable" section, keeping the declaration section clean. That's what "readable" to me. > And it seems to me a "const" should not be changed by code. ;-) Even if it's allowed, you're declaring typed constant, which is not a real constant and has the same effect as initialized variable (e.g. you can change the value later on). It only differs (from initialized var) when the assignment happens inside a routine.

Delphi has {$J-} in order to set them readonly.
Another problem with Free Pascal in this context is that it is AFAIK not possible to define forward types which could be used as parameters for procedures used as procedure variables in later type definitions.

Martin

Reply by email

Sven Barth
to Martin Schreiber
Martin Schreiber wrote 12 lines
On Tuesday 19 March 2013 09:06:31 leledumbo wrote: >> Really? No gain in the beloved readability? > Not for me, I always initialize code in "executable" section, keeping the > declaration section clean. That's what "readable" to me. > >> And it seems to me a "const" should not be changed by code. ;-) > Even if it's allowed, you're declaring typed constant, which is not a real > constant and has the same effect as initialized variable (e.g. you can > change the value later on). It only differs (from initialized var) when the > assignment happens inside a routine. > Delphi has {$J-} in order to set them readonly.

Do you know whether Delphi has by default $J+ or $J-?

Another problem with Free Pascal in this context is that it is AFAIK not possible to define forward types which could be used as parameters for procedures used as procedure variables in later type definitions.

In case of records, classes and objects you can work around this with nested types (Note: for records the modeswitch advancedrecords is required in non-Delphi modes):

=== code begin ===

type
   TMyRecord = record
   public type
      TTest = procedure(aRec: TMyRecord);
   public var
     Test: TTest;
   end;

=== code end ===

And I just noticed: this example does not work in Delphi...

Regards,
Sven

Reply by email

Martin Schreiber
to Sven Barth: Do you know whether Delphi has by default $J+ or $J-? In case of records, classes and…
Sven Barth wrote 5 lines
> > Delphi has {$J-} in order to set them readonly. Do you know whether Delphi has by default $J+ or $J-?
{$J+} in Delphi 7.
Show quoted text 20 lines
> Another problem with Free Pascal in this context is that it is AFAIK not > possible to define forward types which could be used as parameters for > procedures used as procedure variables in later type definitions. In case of records, classes and objects you can work around this with nested types (Note: for records the modeswitch advancedrecords is required in non-Delphi modes): === code begin === type TMyRecord = record public type TTest = procedure(aRec: TMyRecord); public var Test: TTest; end; === code end ===

Ouch! ;-)

Martin

Reply by email