Sign in Join

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

Mailing lists / fpc-pascal / 2008-10

Typed constant

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Valdas Jankūnas

Hello,

code snippet from my app:

type
   TLaikoJuosta=record
       Valanda, Minute: SmallInt;
   end;
const
   MAX_LAIKO_JUOSTA: TLaikoJuosta=(Valanda: 99; Minute: 59);
  I want create another constant:
MAX_LAIKO_JUOSTA_MINUTEMIS=MAX_LAIKO_JUOSTA.Valanda*MinsPerHour+MAX_LAIKO_JUOSTA.Minute;

But compiler (I working in Lazarus v0.9.27 r16994 i386-linux-gtk 2 (beta); fpc 2.2.2) reported "Error: Illegal expression" and cursor blinks at end of line of new constant. Same error if I try:

MAX_LAIKO_JUOSTA_MINUTEMIS:
SmallInt=MAX_LAIKO_JUOSTA.Valanda*MinsPerHour+MAX_LAIKO_JUOSTA.Minute;
  I creating constant from a constant. But why I getting that error?
Valdas Jankūnas

Reply by email

Jonas Maebe
to Valdas Jankūnas
Valdas Jankūnas wrote 16 lines
const MAX_LAIKO_JUOSTA: TLaikoJuosta=(Valanda: 99; Minute: 59); I want create another constant: MAX_LAIKO_JUOSTA_MINUTEMIS=MAX_LAIKO_JUOSTA.Valanda*MinsPerHour +MAX_LAIKO_JUOSTA.Minute; But compiler (I working in Lazarus v0.9.27 r16994 i386-linux-gtk 2 (beta); fpc 2.2.2) reported "Error: Illegal expression" and cursor blinks at end of line of new constant. Same error if I try: MAX_LAIKO_JUOSTA_MINUTEMIS: SmallInt=MAX_LAIKO_JUOSTA.Valanda*MinsPerHour+MAX_LAIKO_JUOSTA.Minute; I creating constant from a constant. But why I getting that error?

Because a "typed constant" is a bad name chose by Borland for "initialised variable". And you cannot use variables (initialised or not) in constant expressions.

With {$j-} typed constants become real constants (they cannot be changed anymore, unlike in the default {$j+} state), but even then the above probably won't work because while parsing the compiler directly writes the values of such constants/initialised variables in the assembler list rather than constructing some semantic entity which can be reused later (so the compiler has no way to retrieve the values of such constants when you try to reuse them later on).

Jonas

Reply by email

Valdas Jankūnas
to Jonas Maebe

Jonas Maebe rašė:

Show quoted text 16 lines
> MAX_LAIKO_JUOSTA_MINUTEMIS: > SmallInt=MAX_LAIKO_JUOSTA.Valanda*MinsPerHour+MAX_LAIKO_JUOSTA.Minute; > > I creating constant from a constant. But why I getting that error? Because a "typed constant" is a bad name chose by Borland for "initialised variable". And you cannot use variables (initialised or not) in constant expressions. With {$j-} typed constants become real constants (they cannot be changed anymore, unlike in the default {$j+} state), but even then the above probably won't work because while parsing the compiler directly writes the values of such constants/initialised variables in the assembler list rather than constructing some semantic entity which can be reused later (so the compiler has no way to retrieve the values of such constants when you try to reuse them later on).

OK. Thanks for explanation

Valdas Jankūnas

Reply by email