Sign in Join

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

Mailing lists / fpc-pascal / 2019-06

Using constants in constant record definition

7 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

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

Simon Ameis

Hello,

I'm just porting some C code to pascal. This code contains constant structure definitions which use other constants of simple types.

However FPC reports "Error: Illegal expression" when using a constant within an constant record definition.

program Project1;
type
  TMyRecord = record
    x: Integer;
  end;
const                  
  MYRECORDCONST2: TMyRecord = (x: 5); //< compiles fine
  MYINTEGERCONST: Integer = 5;        //< defining integer const
  MYRECORDCONST1: TMyRecord = (x: MYINTEGERCONST); //< using constant is
no possible
begin
end.

Is this intended or should I fill in a bug report? If it's intendet, I'm wondering why?

Regards
Simon Ameis

Reply by email

Jonas Maebe
to Simon Ameis
Simon Ameis wrote 5 lines
const   MYRECORDCONST2: TMyRecord = (x: 5); //< compiles fine   MYINTEGERCONST: Integer = 5;        //< defining integer const   MYRECORDCONST1: TMyRecord = (x: MYINTEGERCONST); //< using constant is no possible

For historical reasons, "MYINTEGERCONST: Integer = 5" does not declare a constant, but an initialised variable. To declare a constant, use

const
   MYINTEGERCONST = 5;

If you want to explicitly specify its type, you can use

const
   MYINTEGERCONST = Integer(5);

Jonas

Reply by email

Marco van de Voort
to Simon Ameis: I'm just porting some C code to pascal. This code contains constant structure definitions…

Op 2019-06-12 om 22:25 schreef Simon Ameis:

Show quoted text 10 lines
const   MYRECORDCONST2: TMyRecord = (x: 5); //< compiles fine   MYINTEGERCONST: Integer = 5;        //< defining integer const   MYRECORDCONST1: TMyRecord = (x: MYINTEGERCONST); //< using constant is no possible begin end. Is this intended or should I fill in a bug report? If it's intendet, I'm wondering why?

myintegerconst is a typed initialized variable depending on the $J state.

Use

   MYINTEGERCONST = Integer ( 5);

to define a true integer const. True consts don't occupy memory.

Reply by email

gabor
to Simon Ameis: I'm just porting some C code to pascal. This code contains constant structure definitions…

Hello
Typed constants work just like initialized variables. Maybethis is the problem?

Try:
MYINTEGERCONST = 5;
or
MYINTEGERCONST = Integer(5);

Regards,
Michał.

W dniu 2019-06-12 o 22:25, Simon Ameis pisze:

Show quoted text 33 lines
Hello, I'm just porting some C code to pascal. This code contains constant structure definitions which use other constants of simple types. However FPC reports "Error: Illegal expression" when using a constant within an constant record definition. program Project1; type   TMyRecord = record     x: Integer;   end; const   MYRECORDCONST2: TMyRecord = (x: 5); //< compiles fine   MYINTEGERCONST: Integer = 5;        //< defining integer const   MYRECORDCONST1: TMyRecord = (x: MYINTEGERCONST); //< using constant is no possible begin end. Is this intended or should I fill in a bug report? If it's intendet, I'm wondering why? Regards Simon Ameis _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Dejan Boras
to Simon Ameis: I'm just porting some C code to pascal. This code contains constant structure definitions…

It's not a bug. There's no support in FPC/Pascal for using constants with a defined type in other declarations.
Define your MYINTEGERCONST without a type, and it should work.

const
  MYINTEGERCONST = 5;
PM Simon Ameis wrote 33 lines
Hello, I'm just porting some C code to pascal. This code contains constant structure definitions which use other constants of simple types. However FPC reports "Error: Illegal expression" when using a constant within an constant record definition. program Project1; type TMyRecord = record x: Integer; end; const MYRECORDCONST2: TMyRecord = (x: 5); //< compiles fine MYINTEGERCONST: Integer = 5; //< defining integer const MYRECORDCONST1: TMyRecord = (x: MYINTEGERCONST); //< using constant is no possible begin end. Is this intended or should I fill in a bug report? If it's intendet, I'm wondering why? Regards Simon Ameis _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Simon Ameis
to Dejan Boras
Dejan Boras wrote
It's not a bug. There's no support in FPC/Pascal for using constants with a defined type in other declarations. Define your MYINTEGERCONST without a type, and it should work.

Thanks all. It was a bit late and while working with existent code, I didn't came up with this idea.

The compiler switch {$WriteableConst OFF} doesn't change anything in regards to typed constants. The only way is to use real constants which may have an explicit typecast.

Reply by email