Recent

Author Topic: Inc operator overloading problem with latest FPC  (Read 2176 times)

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Inc operator overloading problem with latest FPC
« on: December 09, 2023, 02:37:11 pm »
I'm using Inc operator overloading with advanced records, with Delphi mode set at the top of the unit source file.

After installing the latest Lazarus v2.2.6 with FPC v3.2.2 I can no longer overload the Inc with two parameters, the Inc with single parameter works ok. I've tried FPC standalone from the command line and get the same problem. My understanding is that seting Delphi mode in the source code should allow this feature to work.

This works ok with Lazarus 2.0.12 which uses FPC v3.2.0

The odd thing is, if I copy the Lazarus configuration files from Lazarus 2.0.12 to Lazarus v2.2.6, then let Lazarus "convert" them, Lazarus-2.2.6/FPC-3.2.2 will then compile the code ok.
I've looked at the Lazarus config files with a compare utility, to try to figure out if there's a difference in the FPC config between the two installations, but the file content is too complicated for me to understand.

I've had to remove the two parameter Inc functionality from the code with a {$define} because no-one else will be able to compile the code.

Any help or suggestions are welcome.

Thaddy

  • Hero Member
  • *****
  • Posts: 13983
  • Probably until I exterminate Putin.
Re: Inc operator overloading problem with latest FPC
« Reply #1 on: December 09, 2023, 03:00:56 pm »
HOW are you using the second parameter? I suspect you make similar mistakes as in your other thread.

There are no issues.
« Last Edit: December 09, 2023, 03:12:18 pm by Thaddy »
Specialize a type, not a var.

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Re: Inc operator overloading problem with latest FPC
« Reply #2 on: December 09, 2023, 03:32:49 pm »
I suspect you make similar mistakes as in your other thread.
Yes, I do make mistakes   :o
But in this instance, I don't think this is my mistake for the following reasons...
- the exact same code compiles ok with Lazarus-2.0.12/FPC-3.2.0
- after I copy the Lazarus config files from v2.0.12 to v2.2.6, the code compiles ok
If the problem was in my code, I don't think the above would fix the problem.

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Re: Inc operator overloading problem with latest FPC
« Reply #3 on: December 09, 2023, 03:37:55 pm »
My guess is that it might be a configuration problem. I tried, but could not find a likely configuration suspect.

Thaddy

  • Hero Member
  • *****
  • Posts: 13983
  • Probably until I exterminate Putin.
Re: Inc operator overloading problem with latest FPC
« Reply #4 on: December 09, 2023, 06:21:43 pm »
 The most common mistake with inc is that inc already takes the first parameter size into account, say, a record size. If you then use the second parameter with, say, length, then you would jump into a wrong place.
inc takes into account the size, even with records. That can be confusing, but is actually sheer logic and a very welcome Pascal feature.
Let me know if this was the error cause....

Most other languages do not have this feature, at least not at the compiler level. Sometimes through library level, but not often.

I have to give you that you have a way to always guess wrong and then blame the compiler. Better keep asking questions for the time being. :-X
« Last Edit: December 09, 2023, 06:31:44 pm by Thaddy »
Specialize a type, not a var.

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Re: Inc operator overloading problem with latest FPC
« Reply #5 on: December 09, 2023, 07:17:50 pm »
Here is the smallest/simplest test program I could make, that illustrates the problem:
Code: Pascal  [Select][+][-]
  1. {$MODE DELPHI}
  2. program test_inc_2;
  3. uses sysutils;
  4. type T_R=record
  5.         private
  6.         public
  7.                 V:int32;
  8.                 class operator inc(const v1:T_R):T_R;
  9.                 class operator inc(const v1,v2:T_R):T_R;        // <--- this is the line that fails
  10.                 class operator implicit(const v1:integer):T_R;
  11.                 class operator implicit(const v1:T_R):string;
  12.         end;
  13. var     v1,v2   :T_R;
  14. class operator T_R.implicit(const v1:integer):T_R;
  15. begin Result.V:= v1; end;
  16. class operator T_R.implicit(const v1:T_R):string;
  17. begin Result:= inttostr(v1.V); end;
  18. class operator T_R.inc(const v1:T_R):T_R;
  19. begin Result:= v1; Inc(Result.V); end;
  20. class operator T_R.inc(const v1,v2:T_R):T_R;
  21. begin Result:= v1; Result.V:= Result.V + v2.V; end;
  22.  
  23. begin
  24. v1:= 1; v2:= 2;
  25. writeln('v1 = ',string(v1));
  26. writeln('v2 = ',string(v2));
  27. Inc(v1,v2);
  28. writeln('v1 = ',string(v1));
  29. end.
Here is the output from the FPC 3.2.0
Code: Text  [Select][+][-]
  1. $ fpc test_inc_2.pas
  2. Free Pascal Compiler version 3.2.0 [2020/06/04] for i386
  3. Copyright (c) 1993-2020 by Florian Klaempfl and others
  4. Target OS: Win32 for i386
  5. Compiling test_inc_2.pas
  6. Linking test_inc_2.exe
  7. 30 lines compiled, 0.1 sec, 67824 bytes code, 4244 bytes data
Here is the output from the test program that shows the code is working:
Code: Text  [Select][+][-]
  1. $ ./test_inc_2
  2. v1 = 1
  3. v2 = 2
  4. v1 = 3
Here is the output from FPC 3.2.2
Code: Text  [Select][+][-]
  1. $ fpc test_inc_2.pas
  2. Free Pascal Compiler version 3.2.2 [2021/05/15] for i386
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Win32 for i386
  5. Compiling test_inc_2.pas
  6. test_inc_2.pas(11,46) Error: Impossible operator overload
  7. test_inc_2.pas(51) Fatal: There were 1 errors compiling module, stopping
  8. Fatal: Compilation aborted
  9. Error: D:\Programs\Cygwin64\home\Mark\FPC\bin\i386-Win32\ppc386.exe returned an error exitcode
If I remove the two-parameter inc from the test program, like this:
Code: Pascal  [Select][+][-]
  1. {$MODE DELPHI}
  2. program test_inc_2;
  3. uses sysutils;
  4. type T_R=record
  5.         private
  6.         public
  7.                 V:int32;
  8.                 class operator inc(const v1:T_R):T_R;
  9.                 class operator implicit(const v1:integer):T_R;
  10.                 class operator implicit(const v1:T_R):string;
  11.         end;
  12. var     v1      :T_R;
  13. class operator T_R.implicit(const v1:integer):T_R;
  14. begin Result.V:= v1; end;
  15. class operator T_R.implicit(const v1:T_R):string;
  16. begin Result:= inttostr(v1.V); end;
  17. class operator T_R.inc(const v1:T_R):T_R;
  18. begin Result:= v1; Inc(Result.V); end;
  19.  
  20. begin
  21. v1:= 1;
  22. writeln('v1 = ',string(v1));
  23. Inc(v1);
  24. writeln('v1 = ',string(v1));
  25. end.
It compiles with FPC 3.2.2:
Code: Text  [Select][+][-]
  1. $ fpc test_inc_1.pas
  2. Free Pascal Compiler version 3.2.2 [2021/05/15] for i386
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Win32 for i386
  5. Compiling test_inc_1.pas
  6. Linking test_inc_1.exe
  7. 26 lines compiled, 0.1 sec, 68256 bytes code, 4260 bytes data
and runs as expected:
Code: Text  [Select][+][-]
  1. $ ./test_inc_1
  2. v1 = 1
  3. v1 = 2
If you can see a problem with the code, please can you explain. Thanks.
« Last Edit: December 09, 2023, 07:34:49 pm by ad1mt »

TRon

  • Hero Member
  • *****
  • Posts: 2230
Re: Inc operator overloading problem with latest FPC
« Reply #6 on: December 09, 2023, 09:22:22 pm »
Here is the smallest/simplest test program I could make, that illustrates the problem:
I do not have knowledge on later/newer Delphi versions so can't test/check.

Looking at the documentation on inc/dec operator overloading in Delphi it is explicitly mentioned as a unary operation. So from that I have to take the guess that in case it worked before then that would have to be considered a bug which is now corrected ?

I could not find it explicitly mentioned in the 3.2.2 user changes (I would have expected it would in case my guessing above is correct).

Note that this mention is not the same as an advanced record operator.
« Last Edit: December 09, 2023, 09:24:49 pm by TRon »

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Re: Inc operator overloading problem with latest FPC
« Reply #7 on: December 10, 2023, 09:04:12 am »
The odd thing is, if I copy the Lazarus configuration files from Lazarus 2.0.12 to Lazarus v2.2.6, then let Lazarus "convert" them, Lazarus-2.2.6/FPC-3.2.2 will then compile the code ok.
I've just checked why this works... copying the config files tells Lazarus 2.2.6 to use the FPC inside the Lazarus 2.0.12 (which is FPC 3.2.0)
This is not a big problem for my project, so I'm not going to pursue this further.
« Last Edit: December 10, 2023, 09:34:37 am by ad1mt »

Thaddy

  • Hero Member
  • *****
  • Posts: 13983
  • Probably until I exterminate Putin.
Re: Inc operator overloading problem with latest FPC
« Reply #8 on: December 10, 2023, 09:59:16 am »
The mistake was exactly as I expected and already described.
inc v1,v2 is not correct.It increases with the size of v2 and that is not what you mean. Declare v2 as an integer fixes the overload.
You may want to reread my previous answer.

Suppose the size of the record is 4096. Then, if it works at all,  the array skips to record nbr 4097 and probably overflow. What you want is go to the Nth element in the array. the simple inc simply goes to the next element. The overload with the extra parameter needs that parameter to be of type integer. It is as an index.


A side note, protect against overflow either by exception handling and or by using inrange or ensurerange or a simple test for low and high before executing the increment. Yes also test Low, because the index parameter can be negative. I would go for catching ERangeError which you may or may not get automatically.
« Last Edit: December 10, 2023, 11:04:15 am by Thaddy »
Specialize a type, not a var.

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Re: Inc operator overloading problem with latest FPC
« Reply #9 on: December 10, 2023, 02:53:28 pm »
I've worked around the problem, by removing the inc operator definitions from the extended record definition, and defining them separately as overloaded procedures, like this:
Code: Pascal  [Select][+][-]
  1. {$MODE DELPHI}
  2. program test_inc_3_2_2;
  3. uses sysutils;
  4. type T_R =
  5.         record
  6.         private
  7.         public
  8.                 v:integer;
  9.                 class operator implicit(const v1:integer):T_R;
  10.                 class operator implicit(const v1:T_R):string;
  11.         end;
  12. class operator T_R.implicit(const v1:integer):T_R;
  13. begin Result.v:= v1; end;
  14. class operator T_R.implicit(const v1:T_R):string;
  15. begin Result:= inttostr(v1.v); end;
  16. procedure inc(var v1:T_R); overload;
  17. begin v1.v:= (v1.v + 1); end;
  18. procedure inc(var v1:T_R; const v2:T_R); overload;
  19. begin v1.v:= (v1.v + v2.v); end;
  20.  
  21. var v1,v2       :T_R;
  22. begin
  23. v1:= 1; v2:= 2;
  24. writeln('v1 = ',string(v1));
  25. writeln('v2 = ',string(v2));
  26. inc(v1);
  27. writeln('Inc(v1) = ',string(v1));
  28. Inc(v1,v2);
  29. writeln('Inc(v1,v2) = ',string(v1));
  30. end.
But now I hit another different problem... overloading inc with a procedure hides the standard system inc.

Thaddy

  • Hero Member
  • *****
  • Posts: 13983
  • Probably until I exterminate Putin.
Re: Inc operator overloading problem with latest FPC
« Reply #10 on: December 10, 2023, 02:55:43 pm »
Oh, well. Just do what I explained. Not wasting more time on it.
Specialize a type, not a var.

TRon

  • Hero Member
  • *****
  • Posts: 2230
Re: Inc operator overloading problem with latest FPC
« Reply #11 on: December 10, 2023, 03:15:38 pm »
But now I hit another different problem... overloading inc with a procedure hides the standard system inc.
Then be explicit about which of the different (now) existing version you wish to invoke.

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Re: Inc operator overloading problem with latest FPC
« Reply #12 on: December 10, 2023, 04:42:22 pm »
inc v1,v2 is not correct.It increases with the size of v2 and that is not what you mean. Declare v2 as an integer fixes the overload.
I'm not sure what you're suggesting here. Is it something like this:
Code: Pascal  [Select][+][-]
  1. {$MODE DELPHI}
  2. program test_inc_3_2_2B;
  3. uses sysutils;
  4. type T_R =
  5.         record
  6.         private
  7.         v:integer;
  8.         public
  9.         class operator implicit(const v1:integer):T_R;
  10.         class operator implicit(const v1:T_R):string;
  11.         class operator inc(var v1:T_R;const v2:integer):T_R;  //  <--- 2nd parameter defined as integer
  12.         end;
  13. class operator T_R.implicit(const v1:integer):T_R;
  14. begin Result.v:= v1; end;
  15. class operator T_R.implicit(const v1:T_R):string;
  16. begin Result:= inttostr(v1.v); end;
  17. class operator inc(var v1:T_R;const v2:integer):T_R;  //  <--- 2nd parameter defined as integer
  18. begin v1.v:= (v1.v + v2); end;
  19.  
  20. var v:T_R;
  21. begin
  22. inc(v,1);
  23. end.
If not then maybe you give me a small piece of example code that I can run.
Thanks.
« Last Edit: December 10, 2023, 06:34:59 pm by ad1mt »

Thaddy

  • Hero Member
  • *****
  • Posts: 13983
  • Probably until I exterminate Putin.
Re: Inc operator overloading problem with latest FPC
« Reply #13 on: December 10, 2023, 06:17:50 pm »
NO. the second parameter needs to be an index.
I am sure everybody else understands what I wrote.

So go back to your first example and make v2 an integer and not of type of the record. Then everything will be OK.

Actually, strictly speaking, depending how you implemented it, which you did not, you can also write a version that takes two records, but then you have to write custom code that would never be really helpful.

But I would ignore the last remark and simply implement the V2 parameter as an integer because that is what you exoect and what you want, This is not rocket science, but you simply refuse to read what needs to be done and that is rather annoying.
I am on Tizen OS right now,  and struggling with my keyboard layout not working properly, otherwise I would have fixed your first example already. When I have access to my laptop instead of my Samsung TV      I will add an example. It is a minutes work.
« Last Edit: December 10, 2023, 06:30:14 pm by Thaddy »
Specialize a type, not a var.

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Re: Inc operator overloading problem with latest FPC
« Reply #14 on: December 10, 2023, 06:39:29 pm »
So go back to your first example and make v2 an integer and not of type of the record.
I thought that was what I had done in the code I posted above:
Code: Pascal  [Select][+][-]
  1. class operator inc(var v1:T_R; const v2:integer):T_R;
Sorry if I am getting confused.

 

TinyPortal © 2005-2018