Recent

Author Topic: Compiler can't determine what function to call  (Read 771 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 359
Compiler can't determine what function to call
« on: February 03, 2025, 03:07:49 pm »
Hello.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. type
  4.         TMyType = array [0..15] of Byte;
  5.  
  6. function Length(constref BA: TMyType): SizeInt;
  7. begin
  8.  
  9. end;
  10.  
  11. begin
  12.         Writeln(
  13.                 Length('123')
  14.         );
  15. end.
Compiler version 3.2.2. Is this bug or what?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: Compiler can't determine what function to call
« Reply #1 on: February 03, 2025, 03:47:27 pm »
C&P your code i don't get "Compiler can't determine what function to call"
i get "project1.lpr(14,29) Error: Incompatible type for arg no. 1: Got "Constant String", expected "TMyType""

So what's your question?
Were you expecting the code to call the "standard" length-Function?
In that case prepend your call with "System"
Code: Pascal  [Select][+][-]
  1. System.Length('123')
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

Thaddy

  • Hero Member
  • *****
  • Posts: 18314
  • Here stood a man who saw the Elbe and jumped it.
Re: Compiler can't determine what function to call
« Reply #2 on: February 03, 2025, 03:47:34 pm »
It may well be a bug, not sure.
But you are trying to confuse the compiler on purpose.
The solution is easy:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. type
  4.   TMyType = array [0..15] of byte;
  5.  
  6. function Length(constref BA: TMyType): SizeInt;
  7. begin
  8.   result := 0;
  9. end;
  10.  
  11. begin
  12.   Writeln(System.Length('123'));// make it unambiguous.
  13. end.
Note that if that code would be in a unit, there is no error and the system version is chosen.
Of course, if you change byte to char, your version is chosen provided you specify a result.

@Zvoni
Was easy to replicate on windows x86_64
Posts crossed.
It may very well be a platform bug.
« Last Edit: February 03, 2025, 03:50:21 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Thaddy

  • Hero Member
  • *****
  • Posts: 18314
  • Here stood a man who saw the Elbe and jumped it.
Re: Compiler can't determine what function to call
« Reply #3 on: February 03, 2025, 03:54:09 pm »
It is not platform, so it is a bug. Work-around obvious, but a byte is not a char in Pascal.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6189
  • Compiler Developer
Re: Compiler can't determine what function to call
« Reply #4 on: February 04, 2025, 10:35:08 pm »
Is this bug or what?

Normally you could solve this by declaring your Length function as overload, however due to System.Length being a compiler intrinsic and not a regular function this does not work. Thus you need to explicitly use System.Length to solve this.

 

TinyPortal © 2005-2018