Procedural type and overloaded functions: why am I getting a compile-
time error?
lazarus / 62-General
----------------------------------------------------------------------
EganSolo · 2021-03-12 05:10
Check the silly little program below: If you try to compile it, you'll
get the error indicated in the comment.
I've checked the section of the Free Pascal Reference Guide on
Procedural Type https://freepascal.org/docs-html/ref/refse17.html and
section 14.9.11 on the overload modifier https://freepascal.org/docs-
html/ref/refsu81.html#x201-22300014.9.11 , and neither mention a
conflict between overloading functions and procedural types. Is that
mentioned somewhere else in the manual?
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i Program
Project1; Type MyFuncType = function(const S : String) :
Boolean; Function A(const S: String; const N: integer): Boolean;
overload;begin Result := (N = 0) and (S <> 'honk');End; Function
A(const S: String): Boolean; overload;begin Result := S <>
'honk';End; Procedure DoSomething(const S : String; const aFunc :
MyFuncType);begin If aFunc(S) then Writeln('I'' not honking') else
Writeln('I''m honking');end; Begin DoSomething('honk',@A); //<--:
Compile Error: Got "
" + //<--:
expected "" Readln;End.
----------------------------------------------------------------------
balazsszekely · 2021-03-12 05:42
@EganSolo
QuoteCheck the silly little program below: If you try to compile it,
you'll get the error indicated in the comment.{$mode ObjFPC} is more
restrictive. Please try this:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i Program
Project1; Type MyFuncType = function(const S : String) :
Boolean; Function A(const S: String; const N: integer): Boolean;
overload;begin Result := (N = 0) and (S <> 'honk');End; Function
A(const S: String): Boolean; overload;begin Result := S <>
'honk';End; Procedure DoSomething(const S : String; const aFunc :
MyFuncType);begin If aFunc(S) then Writeln('I'' not honking') else
Writeln('I''m honking');end; var MyFunc: MyFuncType;Begin MyFunc :=
@A; DoSomething('honk', MyFunc); Readln;End.
If you switch to {$mode delphi}, your code will work fine. As a side
note your forgot "const S" from MyFuncType = function(S : String) :
Boolean.
----------------------------------------------------------------------
EganSolo · 2021-03-12 06:38
Got it. Thanks for the quick response GetMem. Would it make sense to
amend the page in the doc about mod ObjFPC to indicate this? I'm
referring to this page: https://www.freepascal.org/docs-
html/prog/progse72.html#x287-303000D.1.
Also, I've fixed my type (missing Const) in the initial code.
----------------------------------------------------------------------
balazsszekely · 2021-03-12 07:03
@EganSolo
QuoteWould it make sense to amend the page in the doc about mod ObjFPC
to indicate this? I'm referring to this page:
https://www.freepascal.org/docs-
html/prog/progse72.html#x287-303000D.1.I'm not sure what exactly we
should amend. Mode ObjFPC differs a lot from mode delphi.
----------------------------------------------------------------------
EganSolo · 2021-03-12 07:22
I mean, would it make sense to list the differences that programmers
are likely to encounter? Perhaps the top ten or fifteen? Just a
thought.
----------------------------------------------------------------------
dseligo · 2021-03-12 07:33
Quote from: EganSolo on March 12, 2021, 08:22:12 amI mean, would it
make sense to list the differences that programmers are likely to
encounter? Perhaps the top ten or fifteen? Just a thought.
Check this link:
https://wiki.freepascal.org/Code_Conversion_Guide#Syntax_differences
And some more about modes: https://wiki.freepascal.org/Compiler_Mode
----------------------------------------------------------------------
howardpc · 2021-03-12 08:31
It is worth noting that {$Mode fpc} is the compiler default, and is
neither {$Mode objfpc} nor {$Mode Delphi}.
In almost every program understanding and specifying the mode is
critical.