Recent

Author Topic: compiler error when using the same name in class and function.  (Read 1561 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
compiler error when using the same name in class and function.
« on: September 06, 2019, 04:20:44 pm »
If I have a class like this:
Code: Pascal  [Select][+][-]
  1.  TMain = class
  2. private
  3.   fjaar : integer;
  4.   procedure MyFunc;
  5. public
  6.     property jaar  : integer read fjaar write fjaar;
  7. end;
  8.  
And I create a procedure / function like this:
Code: Pascal  [Select][+][-]
  1. procedure TMain.myfunc;
  2. var jaar : integer;
  3. begin
  4.  ....
  5. end;
  6.  
Why will the application not compile? It result with this message:
Quote
clmain.pas(180,5) Error: Duplicate identifier "jaar"

Delphi doesn't have this problem

Currently I'm using Lazarus 2.02 32 bits / fpc 3.04 / win10.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: compiler error when using the same name in class and function.
« Reply #1 on: September 06, 2019, 04:23:01 pm »
Compile using {$ mode DELPHI} and everything will be stew  :D

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: compiler error when using the same name in class and function.
« Reply #2 on: September 06, 2019, 04:25:36 pm »
There is also an Switch you can use if you want to stay with ObjFpc mode

Just can't remember it at the moment.

{$MODESWITCH DUPLICATELOCALS} // Allow local variables in class methods to have the same names asproperties of the class.

Book mark that ;)

https://www.freepascal.org/docs-html/prog/progsu105.html
« Last Edit: September 06, 2019, 04:29:27 pm by jamie »
The only true wisdom is knowing you know nothing

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: compiler error when using the same name in class and function.
« Reply #3 on: September 06, 2019, 04:50:02 pm »
Compile using {$ mode DELPHI} and everything will be stew  :D
That's a workaround not an solution
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: compiler error when using the same name in class and function.
« Reply #4 on: September 06, 2019, 07:06:16 pm »
Compile using {$ mode DELPHI} and everything will be stew  :D
That's a workaround not an solution

It is a solution if you want to use constructions which are allowed in that mode (for compatibility) but not in the stricter ObjFPC. If you still want to use ObjFPC but activate some of those features (like this one or, say, nested comments, or assigning callbacks w/out @, etc.) you'll have to use $modeswitch, as jamie said.

My personal recommendation is to use modeswitch unless you are using lots of Delphi-like features or writing dual-compiling code, in which case I'd use the full {$mode Delphi}. Of course, that's just my opinion; YMMV
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14391
  • Sensorship about opinions does not belong here.
Re: compiler error when using the same name in class and function.
« Reply #5 on: September 06, 2019, 09:01:44 pm »
It is indeed not a work around. objpas mode is simply more strict. {$mode delphi} is good advice if you expect it to work like in Delphi, as OP seems to suggest.
Otherwise he will have a whole slew of modeswitches before long....
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: compiler error when using the same name in class and function.
« Reply #6 on: September 08, 2019, 01:59:40 pm »
Let me say otherwise: Why is ObjFPC causing this error.
In my opinion a local var has a different pointerview / declaration than a global view. a name has nothing to do with compiling. It's just a reference to a location of address in memory.

It's simple to work with. Just gave the variabele another name, but it shouldn't have.

I love to work with ObjFPC. If I use delphi mode a better can stay to Delphi (my point of view)
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 14391
  • Sensorship about opinions does not belong here.
Re: compiler error when using the same name in class and function.
« Reply #7 on: September 08, 2019, 02:26:08 pm »
Let me say otherwise: Why is ObjFPC causing this error.
In my opinion a local var has a different pointerview / declaration than a global view. a name has nothing to do with compiling. It's just a reference to a location of address in memory.

It's simple to work with. Just gave the variabele another name, but it shouldn't have.

I love to work with ObjFPC. If I use delphi mode a better can stay to Delphi (my point of view)
The issue at hand is that it is good programming practice to have unique names, because it can confuse the programmer. It is a helping feature to resolve bugs before they can happen.
As such it is a feature.

Anyway, you can avoid it, - not work around it, it is no work-around  - by using Delphi mode, which would cause that type of errors eventually. Think about scoping errors.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: compiler error when using the same name in class and function.
« Reply #8 on: September 08, 2019, 02:43:46 pm »
Let me say otherwise: Why is ObjFPC causing this error.
Mode obfpc raises this error because potential (corner case) ambiguities such as this were one of the reasons for introducing the objfpc mode, part of its raison d'être.
Pascal programmers fall into several camps, one of which sees no need for flagging (or preventing) such potential ambiguities.  Delphi's compiler developers are definitely in this  camp.

Some of those who develop FPC feel differently, and so introduced a stricter mode.
This is not because the compiler itself has any problems at all with potential ambiguities (genuine ambiguity excepted), being thoroughly deterministic, but because sloppy or careless programming and variable naming style can sooner or later trip up both experienced and rookie programmers.

Some of us need our hand held. Others refuse any such aids, and reuse variable names with apparent impunity.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: compiler error when using the same name in class and function.
« Reply #9 on: September 09, 2019, 01:12:04 pm »
That's an answer I can work with :D
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018