objfpc and delphi mode callbacks lazarus / 62-General ---------------------------------------------------------------------- Zaher · 2018-04-02 17:28 I got error Wrong number of parameters  when i pass callback function in objfpc, and the function declared other unit in delphi mode Error: Wrong number of parameters specified for call to "MyCallback2" Error: Found declaration: MyCallback2(AnsiString); Unit2 Code: [Select]unit Unit2; {$mode delphi}{$H+} interface uses   Classes, SysUtils; type   TMyCallback = procedure(S: string); procedure CallIt(const ACallback: TMyCallback); implementation procedure MyCallback1(S: string); begin end; procedure CallIt(const ACallback: TMyCallback); begin   ACallback('Hello'); end; end. Here where i call it Code: [Select]procedure MyCallback2(S: string); begin end; procedure TForm1.FormCreate(Sender: TObject); begin   CallIt(MyCallback2); end; I need to use CallIt and pass mycallback2 without using @ pointer of procedure, i don't to lose checking parameters benefits , or using delphi mode in unit1. is there any way to solve it? ---------------------------------------------------------------------- molly · 2018-04-02 17:54 Quote from: Zaher on April 02, 2018, 07:28:23 pmI need to use CallIt and pass mycallback2 without using @ pointer of procedure, i don't to lose checking parameters benefits , or using delphi mode in unit1. is there any way to solve it? Code: [Select]{$modeswitch CLASSICPROCVARS} ? More info. ---------------------------------------------------------------------- Zaher · 2018-04-02 18:16 Yes that help, but still need to understand why there is different? Maybe I can change the parameters to suite with callback proc ---------------------------------------------------------------------- rvk · 2018-04-02 18:25 Quote from: Zaher on April 02, 2018, 07:28:23 pmI need to use CallIt and pass mycallback2 without using @ pointer of procedure, i don't to lose checking parameters benefits , or using delphi mode in unit1. I'm not sure I understand... but what's the problem with using @ in this case in unit1? 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 unit Unit1; {$mode objfpc}{$H+}//...procedure TForm1.FormCreate(Sender: TObject);begin  CallIt(@MyCallback2);end; A.F.A.I.K. you don't "lose checking parameters benefits" when passing it with @, or am I missing something? ---------------------------------------------------------------------- Zaher · 2018-04-02 18:29 The problem if I changed the parameters of TMyCallback, I need the compiler stops me, to correct my functions With passing pointer, compiler work, but at runing time, I got AV. Code: [Select]type   TMyCallback = procedure(S: string; ID: Integer); ---------------------------------------------------------------------- rvk · 2018-04-02 18:31 Ah, ok. How about using {$typedaddress on} in unit2? QuoteAlso, if you are using the @-address-operator usage of {$typedaddress on} is advised in order to prevent programming mistakes.http://wiki.freepascal.org/How_to_use_procedural_variables B.T.W. I still got an "Incompatible type for arg no. 1" error even without it. Code below gives me: Quoteunit1.pas(39,22) Error: Incompatible type for arg no. 1: Got "
", expected ""Which seems fine. 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementationuses Unit2; {$R *.lfm} { TForm1 }procedure MyCallback2(S: string);begin  Showmessage(S);end; procedure TForm1.FormCreate(Sender: TObject);begin  CallIt(@MyCallback2);end; end. 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 unit Unit2; {$mode delphi}{$H+} interface uses  Classes, SysUtils; type  TMyCallback = procedure(S: string; ID: Integer); procedure CallIt(const ACallback: TMyCallback); implementation procedure CallIt(const ACallback: TMyCallback);begin  ACallback('Hello', 2);end; end. ---------------------------------------------------------------------- Zaher · 2018-04-02 18:52 Oh, my fault, Yes when i used @ the compiler still check the params, in objfpc, It is my fault, but i am sure I tested it before posting here O.o