Recent

Author Topic: Duplicate identifier error on typed const declaration  (Read 639 times)

koch

  • Newbie
  • Posts: 6
Duplicate identifier error on typed const declaration
« on: February 16, 2026, 01:21:23 pm »
Hi.
Example in Lazarus v.4.4:

Code: Pascal  [Select][+][-]
  1. unit foo;
  2.  
  3. {$mode ObjFpc}
  4.  
  5. interface
  6.  
  7. type
  8.  
  9.   TParent = class
  10.   strict private var
  11.     FVar: string;
  12.  
  13.   strict private const
  14.     FConst = '';
  15.     FTypedConst: string = '';
  16.   end;
  17.  
  18.   TChild = class(TParent)
  19.   strict private var
  20.     FVar: string;
  21.  
  22.   strict private const
  23.     FConst = '';
  24.     FTypedConst: string = '';
  25.   end;
  26.  
  27. implementation
  28.  

Why I have compiler error "Error: Duplicate identifier "FTypedConst"" ?

With switch {$mode delphi} it is not occured.
Can I remove compiler error without using switch {$mode delphi} ???


n7800

  • Hero Member
  • *****
  • Posts: 650
  • Lazarus IDE contributor
    • GitLab profile
Re: Duplicate identifier error on typed const declaration
« Reply #1 on: February 16, 2026, 02:26:57 pm »
Just a first glance... You're using a typed constant. If you replace it with a regular constant (in the base class), everything compiles:
Code: Diff  [Select][+][-]
  1. -  FTypedConst: string = '';
  2. +  FTypedConst = '';

The behavior in FPC trunk is the same. I don't know if this is a bug, maybe someone can clarify. I'm too lazy to look at the documentation or the bug tracker right now ))

cdbc

  • Hero Member
  • *****
  • Posts: 2687
    • http://www.cdbc.dk
Re: Duplicate identifier error on typed const declaration
« Reply #2 on: February 16, 2026, 02:39:03 pm »
Hi
I don't think it's a bug, because this "FTypedConst: string = '';" is a (?global?) static var a.k.a. writable const (with embarcadero's poor naming)
Whereas the change to a normal const, makes it 'strict private'
...I think  %)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Zvoni

  • Hero Member
  • *****
  • Posts: 3315
Re: Duplicate identifier error on typed const declaration
« Reply #3 on: February 16, 2026, 02:40:08 pm »
at a guess: It's allowed in Delphi, in ObjFPC it's not.

makes it 99.99999% sure it's not a Bug

EDIT
Quote
Whereas the change to a normal const, makes it 'strict private'

An untyped constant is resolved during compile-time, a.k.a. the compiler replaces the Constant expression with its defined value.

A typed constant has an address in memory, which is only known at runtime

« Last Edit: February 16, 2026, 02:44:48 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

koch

  • Newbie
  • Posts: 6
Re: Duplicate identifier error on typed const declaration
« Reply #4 on: February 16, 2026, 02:42:22 pm »
You can see regular constant in my example and I khow what they without error. But I need typed const and my question about it.
My example is very simple and only for understanding this situation. In real I use more difficult typed constants...

What you say about the next example?
Code: Pascal  [Select][+][-]
  1. strict private type
  2.     TMyRecord = record
  3.       Param1 : string;
  4.       Param2 : string;
  5.     end;
  6.  
  7. strict private const
  8.   FTypedConst: TMyRecord = (
  9.       Param1 : '';
  10.       Param2 : '';
  11.   );
  12.  



Zvoni

  • Hero Member
  • *****
  • Posts: 3315
Re: Duplicate identifier error on typed const declaration
« Reply #5 on: February 16, 2026, 02:46:44 pm »
The only workaround i can think of (to avoid Mode Delphi) is to make it a Read-Only Property
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

koch

  • Newbie
  • Posts: 6
Re: Duplicate identifier error on typed const declaration
« Reply #6 on: February 16, 2026, 02:52:39 pm »
i think it is a fpc bug.
Delphi compiled it without error


440bx

  • Hero Member
  • *****
  • Posts: 6158
Re: Duplicate identifier error on typed const declaration
« Reply #7 on: February 16, 2026, 03:14:32 pm »
i think it is a fpc bug.
Delphi compiled it without error
It is a bug.

The problem is that FPC attempts to have the typed constant identifier twice in the global data segment and that causes a duplicate identifier error but, the identifier itself isn't global, it only exists in the scope of its respective class which the compiler is failing to take into account.

The data should be stored in the global data segment but, that doesn't make its identifier global.  Considering the identifier global because it is located in the data segment is the bug.  A bug which does NOT occur when using advanced record and/or plain functions, IOW, using advanced records and/or plain functions it is possible to define equally named static variables in different records and/or functions without any problems (as it should be.)    When using inheritance, the compiler isn't determining the scope accurately in the case of a typed constant (it does figure it out correctly for other types, which is why it works for plain constants and plain variables - there is no "scope resolution" involved in those cases.)
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Zvoni

  • Hero Member
  • *****
  • Posts: 3315
Re: Duplicate identifier error on typed const declaration
« Reply #8 on: February 16, 2026, 03:15:30 pm »
i think it is a fpc bug.
Delphi compiled it without error
No.
Just because something compiles in mode Delphi, and not in the others, doesn't constitute a Bug in the other modes.
Plenty of examples for differences (the "famous" one being the @-Operator for function -pointers)

EDIT: Just saw 440bx' answer.....
Hmm.....his reasoning is sound.....

Let's see if one of the compiler-devs will shed some light on it
« Last Edit: February 16, 2026, 03:17:48 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 1674
Re: Duplicate identifier error on typed const declaration
« Reply #9 on: February 16, 2026, 03:19:44 pm »
i think it is a fpc bug.
Delphi compiled it without error

Why don't you use {$mode delphi} then?

Zvoni

  • Hero Member
  • *****
  • Posts: 3315
Re: Duplicate identifier error on typed const declaration
« Reply #10 on: February 16, 2026, 03:23:16 pm »
i think it is a fpc bug.
Delphi compiled it without error

Why don't you use {$mode delphi} then?

He doesn't want to use Mode Delphi (for whatever reasons)
Quote
With switch {$mode delphi} it is not occured.
Can I remove compiler error without using switch {$mode delphi} ???
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

koch

  • Newbie
  • Posts: 6
Re: Duplicate identifier error on typed const declaration
« Reply #11 on: February 16, 2026, 03:56:06 pm »
I try to find in {$modeswitch xxx} options to avoid this situation and can not find nothing.


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12208
  • Debugger - SynEdit - and more
    • wiki
Re: Duplicate identifier error on typed const declaration
« Reply #12 on: February 16, 2026, 04:03:10 pm »
I would suggest to report it as a bug.

Then you will likely at least get a definite answer if it is or is not.

 

TinyPortal © 2005-2018