Sign in Join

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

Mailing lists / fpc-pascal / 2023-03

Illegal expression when redeclare typed constant

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Zamrony P. Juhara
Suppose  I have
unit mytest;
interface
const    MY_VALUE = 1;    MY_INT_VALUE: integer = 1;
implementationend.
and second unit
unit mytest2;
interface
uses mytest;
const    // this is legal    MY_VALUE = mytest.MY_VALUE;
    // this  causes illegal expression    // MY_INT_VALUE = mytest.MY_INT_VALUE;    // MY_INT_VALUE : integer = mytest.MY_INT_VALUE;
implementationend.

Is there workaround to redeclare typed constant in other unit so that I don't need to duplicate code? Thanks
Zamrony P. Juharahttps://juhara.com

Reply by email

Sven Barth
to Zamrony P. Juhara

Zamrony P. Juhara via fpc-pascal schrieb
am So., 26. März 2023, 00:05:

Show quoted text 36 lines
Suppose I have unit mytest; interface const MY_VALUE = 1; MY_INT_VALUE: integer = 1; implementation end. and second unit unit mytest2; interface uses mytest; const // this is legal MY_VALUE = mytest.MY_VALUE; // this causes illegal expression // MY_INT_VALUE = mytest.MY_INT_VALUE; // MY_INT_VALUE : integer = mytest.MY_INT_VALUE; implementation end. Is there workaround to redeclare typed constant in other unit so that I don't need to duplicate code?

Typed constants are essentially variables and you can't redeclare variables.

Only way you might do that (which I do not recommend because it opens it's own can of worms) is to declare the original variable as "public" and to import it in the other unit using "external".

Regards,
Sven

Reply by email