Assigning a static class methods to regular procedural variables lazarus / 62-General ---------------------------------------------------------------------- simone · 2018-01-05 23:26 The reference language guide states that static class methods can be assigned to regular procedural variabile. (https://www.freepascal.org/docs-html/ref/refsu30.html) Nevertheless the following code generate the error: Incompatible types: got "" expected "" Why? Thanks in advance for explanations! 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, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  end;   Handler=class    class procedure OnClick(Sender : TObject); static;  end; var  Form1: TForm1;  Button1 : TButton; implementation {$R *.lfm} class procedure Handler.OnClick(Sender: TObject);begin  ShowMessage('Ok');end; procedure TForm1.FormCreate(Sender: TObject);begin  Button1:=TButton.Create(Self);  Button1.Parent:=Self;  Button1.Left:=100;  Button1.Top:=100;  Button1.OnClick:=@Handler.OnClick;  //<--- ERRORend; end. ---------------------------------------------------------------------- Blaazen · 2018-01-05 23:45 Regular procedural variable probably means 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 TRegProcVar = procedure(Sender: TObject); //of object;i.e. not method. But it does not work too: 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 var aRPV: TRegProcVar;begin  aRPV:=@Handler.OnClick; ---------------------------------------------------------------------- engkin · 2018-01-06 00:03 Regular procedure variable is just a simple pointer. Method variable is a record of two pointers. Static class methods can be assigned to the first type, but you seem you want to assign them to the second type. Here is how to do it: 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 procedure TForm1.FormCreate(Sender: TObject);var  e:TMethod;begin  Button1:=TButton.Create(Self);  Button1.Parent:=Self;  Button1.Left:=100;  Button1.Top:=100;  e.Data:=nil;  e.Code:=@Handler.OnClick;  Button1.OnClick:=TNotifyEvent(e);  //Button1.OnClick:=@Handler.OnClick;  //<--- ERRORend; ---------------------------------------------------------------------- Blaazen · 2018-01-06 00:06 Finally, this works, but requires delphi mode (and remove @): 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 {$mode delphi}{$H+}...TRegProcVar = procedure(Sender: TObject); ...var aRPV: TRegProcVar;begin  aRPV:=Handler.OnClick; ---------------------------------------------------------------------- engkin · 2018-01-06 00:10 Quote from: Blaazen on January 06, 2018, 01:06:00 amFinally, this works, but requires delphi mode (and remove @): 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 {$mode delphi}{$H+}...TRegProcVar = procedure(Sender: TObject); ...var aRPV: TRegProcVar;begin  aRPV:=Handler.OnClick; or: 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   aRPV: TRegProcVar;begin  aRPV:=TRegProcVar(@Handler.OnClick);  aRPV(nil); ---------------------------------------------------------------------- simone · 2018-01-06 11:03 It was my mistake: In the documentation I underestimated the adjective 'regular' refering to procedure. I took into consideration class methods to logically group many event handlers of different controls created dynamically in a program with a complex GUI. Proposed solutions are very interesting from the technical point of view, but in my context could make the code less readable. So I'm looking for others solutions. Thanks! ---------------------------------------------------------------------- Jonas Maebe · 2018-01-06 17:51 No, it was a compiler bug. It has been fixed in the current development version: https://bugs.freepascal.org/view.php?id=27414 ---------------------------------------------------------------------- simone · 2018-01-06 18:51 Thanks Jonas. Although I'm not very familiar with Delphi, I noticed the same issue with 10.2 Starter Edition... ---------------------------------------------------------------------- clauswk · 2018-03-20 02:27 Quote from: Blaazen on January 06, 2018, 01:06:00 amFinally, this works, but requires delphi mode (and remove @): 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 {$mode delphi}{$H+}...TRegProcVar = procedure(Sender: TObject); ...var aRPV: TRegProcVar;begin  aRPV:=Handler.OnClick; Thank you, Blaazen. I have been looking for a solution to a similar problem for a couple of days now, and the delphi mode did it. Claus ---------------------------------------------------------------------- simone · 2023-01-11 15:51 Hi all, I revive this old thread started by me almost five years ago, as I realized that the compiler bug is still present, although at the time it was stated that the same had been fixed. Quote from: Jonas Maebe on January 06, 2018, 06:51:10 pmNo, it was a compiler bug. It has been fixed in the current development version: https://bugs.freepascal.org/view.php?id=27414 ---------------------------------------------------------------------- marcov · 2023-01-11 16:14 if I look at the link, Jonas meant why the code from Blaazen didn't work in non Delphi modes. Your problem is (still) that you try to assign a one pointer type (procedure type or static class method) to a two pointer type (procedure ... OF OBJECT aka TEventNotify;) ---------------------------------------------------------------------- simone · 2023-01-11 16:52 Sorry, but I'm not sure I understand your comment. What do you mean by one/two pointer types? The problem I see is an incompatibility between (static) class methods and callback variables. Thanks for the further explanations. ---------------------------------------------------------------------- marcov · 2023-01-11 17:38 Quote from: simone on January 11, 2023, 05:52:05 pmSorry, but I'm not sure I understand your comment. What do you mean by one/two pointer types? The problem I see is an incompatibility between (static) class methods and callback variables. Thanks for the further explanations. There are multiple callbacks types. e.g. Code: [Select]type     TAProcedureCallback = procedure  (parameter:TParametertype); and Code: [Select]   TAMethodCallback = procedure (parameter:TParametertype) OF OBJECT; A VCL callback like onclick is a TAMethodCallback kind of callback and implemented internally with TWO pointers. One for the address of the code (method) to call, and one for the object reference. While the documented static method compatibility is only with the TAProcedure kind, which is ONE pointer internally. (the address of the code (procedure or static method) to call. In this nomenclature, a method is a procedure that is not static within an object e.g. Type Code: [Select]   type TBLa = class                         procedure thisisamethod;               end; ---------------------------------------------------------------------- simone · 2023-01-11 17:53 Thank you for the explanation, very clear and accurate.  Just one last doubt: in the case of non-static class methods, shouldn't there be compatibility? ---------------------------------------------------------------------- Thaddy · 2023-01-11 18:06 No, because normal methods have a hidden self parameter and a static methods not. ---------------------------------------------------------------------- marcov · 2023-01-11 18:47 As Thaddy says, you have to consider how a callback is called. A compiler is no magic thing, it must work with what information it has, and then document the allowable transitions. There a couple of options: Straight procedure function, no hidden parametersStraight method, virtual or otherwise. Receives "self" aka the object instance as the hidden parameters class method. Receives a point to the class type- descriptor as parameter.  class static methods, as class method, minus the type descriptor. So effectively compatible with procedure If you mix in e.g. calling conventions (register, stdcall and cdecl etc), default parameters, overloading etc, and you can imagine why the so called call-node that handles calling an procedure/method/whatever one of the more complex parts of the compiler :-) ---------------------------------------------------------------------- simone · 2023-01-11 19:15 Thank you so much for discussing the full case study. Considering the implementation aspects illustrated, everything is clear to me now. ---------------------------------------------------------------------- simone · 2023-01-12 12:26 As a final reflection, hoping not to abuse your courtesy, consider the following program: 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;{$Mode ObjFpc}type  TMethodProcedure=procedure of object;   TMyClass=class    class procedure ProcInClass;  end; var  V : TMethodProcedure;   procedure Proc (P : TMethodProcedure);  begin   end; class procedure TMyClass.ProcInClass;begin end; begin  V:=@TMyClass.ProcInClass;      //OK  Proc(@TMyClass.ProcInClass);   //ERRORend. From how the compiler behaves, it emerges that: - I can assign a class method to a (type compatible) variable; - I can't pass a class method as a (type compatible) parameter to a procedure. Is this an expected behavior? Thanks. ---------------------------------------------------------------------- marcov · 2023-01-12 13:20 That's one for PascalDragon I guess. I'd say not. ---------------------------------------------------------------------- simone · 2023-01-12 13:42 I remembered that when I started this thread years ago there was something that was not clear to me. Now I have better focused what does not convince me. I will wait the authoritative opinion of PascalDragon. Thanks a lot again. ---------------------------------------------------------------------- Thaddy · 2023-01-12 13:59 @Simone There a glitch in your code, it should be 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 class procedure TMyClass.ProcInClass;static; // static is important herebegin // and do not let this get optimized awayend;See the manuals, again, https://www.freepascal.org/docs-html/ref/refsu30.html first line. Static class methods have exacly the same signature as a normal procedure or function. They are also not limited to TProc and TFunc. They can be parameterized too. ---------------------------------------------------------------------- simone · 2023-01-12 14:22 Thaddy I read the manual. My question is different. I just want to understand if the compiler behavior in my last example is correct. Like Marcov, I think not. ---------------------------------------------------------------------- bytebites · 2023-01-12 14:24 Add static and get ¨ Quoteproject1.lpr(25,29) Error: Incompatible type for arg no. 1: Got "", expected "" Without static result is Quoteproject1.lpr(25,3) Error: Internal error 200408162 ---------------------------------------------------------------------- Thaddy · 2023-01-12 14:38 TMethod procedure is not compatible with static class procedure. It is simply a procedure: Type TMyProcedure = procedure; ---------------------------------------------------------------------- PascalDragon · 2023-01-12 22:00 Quote from: simone on January 12, 2023, 01:26:21 pmIs this an expected behavior? Thanks. That's a bug (cause it compiles in mode Delphi with the @s removed). However it compiles with 3.3.1, so it's been fixed in the meantime. If someone bisects the commit that fixed it between 3.2.0 and current head then I might be able to merge it to 3.2.3. Quote from: bytebites on January 12, 2023, 03:24:16 pmAdd static and get ¨ Quoteproject1.lpr(25,29) Error: Incompatible type for arg no. 1: Got "", expected "" The compiler should probably add a “static” to the method description to make the issue clearer... Quote from: bytebites on January 12, 2023, 03:24:16 pmWithout static result is Quoteproject1.lpr(25,3) Error: Internal error 200408162 What platform? What compiler version? I don't get an internal error on x86_64-win64 with 3.2.2, only the compile error that simone got. And no error at all with 3.3.1. ---------------------------------------------------------------------- simone · 2023-01-12 22:35 Quote from: PascalDragon on January 12, 2023, 11:00:59 pmQuote from: simone on January 12, 2023, 01:26:21 pmIs this an expected behavior? Thanks. That's a bug (cause it compiles in mode Delphi with the @s removed). However it compiles with 3.3.1, so it's been fixed in the meantime. If someone bisects the commit that fixed it between 3.2.0 and current head then I might be able to merge it to 3.2.3. Thank you PascalDragon and Marcov for your kind and qualified answers, as always. ---------------------------------------------------------------------- bytebites · 2023-01-13 09:19 Linux 64-bit version 3.3.1 ---------------------------------------------------------------------- Thaddy · 2023-01-13 14:09 In trunk/main this is very easy to achieve, btw - and less error prone - by using function references Example: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 {$ifdef mswindows}{$apptype console}{$endif}{$mode delphi}{$modeswitch functionreferences}{$modeswitch anonymousfunctions}{$macro on}{$if FPC_FULLVERSION<30301}{$ERROR this code needs FPC version 3.3.1 later than May 28, 2022 or higher}{$ifend} uses  SysUtils; type  TTestProc = procedure(X: Integer);  TTestMeth = procedure(X: Integer) of object;  TTestRef = reference to procedure(X: Integer); // Ordinary procedureprocedure TestProc(X: Integer);begin  writeln('Ordinary: '#9,X);end; // Methodtype  TMyClass = class    class procedure TestMeth(X: Integer);  end; class procedure TMyClass.TestMeth(X: Integer);begin  writeln('Method: '#9,X);end; {The reference to X can be:1. X2. class method X of object (must be class method)3. reference to X, where X is a procedural type such as procedure(X: Integer). The references are assignment compatible}var   A,B: TTestRef;begin  A := TestProc;  A(1);  A := TMyClass.TestMeth;  A(2);  // test assignment compatibility  B:=A;  A := procedure(X: Integer)        begin          writeln('Anonymous: '#9,X);       end;  A(3);  B(4);end. In the case of a class, this will refuse a normal method of a class, it will only accept class methods. This is also Delphi compatible.