Using constants in constant record definition

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Using constants in constant record definition

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants in constant record definition

Jonas Maebe-3
On 12/06/2019 22:25, Simon Ameis wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants in constant record definition

Marco van de Voort-2
In reply to this post by Simon Ameis

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

> 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants in constant record definition

gabor-10
In reply to this post by Simon Ameis
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:

> 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  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants in constant record definition

Bart-48
In reply to this post by Simon Ameis
On Wed, Jun 12, 2019 at 10:30 PM Simon Ameis <[hidden email]> wrote:

>   MYINTEGERCONST: Integer = 5;        //< defining integer const
It's a writeable constant, so not a constant consta if you get my drift.
Declaring it just as
  MyIntegerConst = 5;
might work just fine. I'm too lazy to chek that for you.

--
Bart
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants in constant record definition

Dejan Boras-2
In reply to this post by Simon Ameis
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;

On Wed, Jun 12, 2019 at 10:30 PM Simon Ameis <[hidden email]> wrote:
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  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants in constant record definition

Simon Ameis
Am 13.06.2019 um 09:34 schrieb Dejan Boras:
> 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal