IInterface issues, compiler can't find members at compile time. This is GDIPLUS lazarus / 21-General ---------------------------------------------------------------------- jamie · 2023-06-05 01:45 I finished an old pet project of making a very common unit floating around for GDIPLUS to work with older fpc. The compiler in question is 3.0.4. 64bit, windows 10. I've done IInterface objects before and they worked find but this GDIPLUS or the compiler has a problem with something. 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.Button1Click(Sender: TObject);var  I:TGDiPlusStartUpInPut;  Token:ULong;  G:TGPGraphics;  P:TGPPen;  C:TGPCOlor;  S:TGPStatus;begin  I.Intialize;  S:=GDiPlusStartUP(Token,@I,Nil);  G:=TGPGraphics.DCCreate(Canvas.Handle);  P:=TGPPen.Create(TGPCOlor.BurlyWood,2);  G.DrawLine(P,0,0,200,100); //<<<<<< Compiler can't find any members of the Interface.  P.Free;  G.Free;  GDIPlusShutdown(Token);end;                                    The code above works basically because up to that line I pointed out, it's calling non interface members. but when it gets to the DRAWLINE or any interface member of G, the graphics interface, the compiler has gone brain dead. unit1.pas(46,5) Error: identifier idents no member "DrawLine"  or any member that happens to be an interface method. 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 DrawLine(const Pen: IGPPen; const Pt1, Pt2: TGPPointF); overload;    procedure DrawLine(const Pen: IGPPen; const X1, Y1, X2, Y2: Single); overload;    procedure DrawLine(const Pen: IGPPen; const Pt1, Pt2: TGPPoint); overload;    procedure DrawLine(const Pen: IGPPen; const X1, Y1, X2, Y2: Integer); overload;   As you can see, there are plenty of DrawLine methods there. Does anyone know why the compiler can't find these methods at compile time? EDIT: Attached is a project and I hope the GDIPLUS came with it. ---------------------------------------------------------------------- WooBean · 2023-06-05 05:59 It may help? This demo is not following environment described by jamie but may point at the real culprit. 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 project;{$mode objFPC}function aa(const a:{int64}integer):boolean; overload;begin  Result:=a>0;end;function aa(const a:single):boolean; overload;begin  Result:=a>0;end; begin  writeln(aa(9999999999)); //project.lpr(13,11) Error: Can't determine which overloaded function to call  ReadLn;end. Maybe const parameter values (X1, Y1, X2, Y2) passed to   procedure DrawLine(const Pen: IGPPen; const X1, Y1, X2, Y2: Single) or   procedure DrawLine(const Pen: IGPPen; const X1, Y1, X2, Y2: Integer) are neither integer nor single? Try G.DrawLine(P,integer(0),integer(0),integer(200),integer(100)); ---------------------------------------------------------------------- jamie · 2023-06-05 10:20 If I access those members within the same unit, "GDIPLUS", then there is no complaint. But it's not liking the idea of jumping unit boundaries. The Code Tools have no issues locating any of those members while editing.  As for the Overload problem, I did have that issue with the constructors, because it couldn't figure out which overload to use. I renamed the problem constructor from CREATE(DC:HDC) to DCCreate(DC:HDC); which solved the problem there.  I will try casting but I don't think that is the issue because I am not getting that message that it can't decide which overload to use, it just can't find any of the INTERFACE members of the class but non- interface members the compiler can see from other units. EDIT:  Casting the constants does not have any effect on the compiler locating the members of the interface. ---------------------------------------------------------------------- jamie · 2023-06-05 12:10 It seems for whatever reason there is a PRIVATE field marker at the start of these Interfaces and the constructors and some other basics are in the PUBLIC section. Code tools can see all of this but the compiler can not from a remote unit? I can't say if making these PUBLIC is the correct fix or even why it was like that. In any case the test project now compiles and executes.  I'll have to ding into this a little more, something strange indeed. ---------------------------------------------------------------------- PascalDragon · 2023-06-05 19:53 The compiler sees the TGPGraphics which indeed has DrawLine declared as private, thus it must not provide access to it (the interface isn't taken into account here). You have to cast TGPGraphics to IGPGraphics so that you can access DrawLine. It's a bug in Lazarus that its CodeTools provide access to DrawLine when used on the TGPGraphics instance. ---------------------------------------------------------------------- jamie · 2023-06-05 23:26 Ok, well I did some reading on the FPC support sights on interfaces etc, and basically it clearly states not to use Private sections in the definitions of the interface, because, like it states, it makes no sense.  As for the compiler, yes, I think that is a mistake but as a reminder I am using a slightly dated Lazarus and that issue may not exists in above 2.0.4 I am using.  The only other issue that couldn't work out was a problem of it not being able to determine which constructor to use so I renamed one of the constructors for now to work around it.  I have made some minor changes to the GDIPLUS file and I will upload a project here with some samples of use. ---------------------------------------------------------------------- PascalDragon · 2023-06-06 20:27 Quote from: jamie on June 06, 2023, 01:26:31 amOk, well I did some reading on the FPC support sights on interfaces etc, and basically it clearly states not to use Private sections in the definitions of the interface, because, like it states, it makes no sense. Where does it state so? And depending on the use case it does indeed make sense: if you want the user to only access these methods using the interface, but not using the class instance itself. Quote from: jamie on June 06, 2023, 01:26:31 am As for the compiler, yes, I think that is a mistake but as a reminder I am using a slightly dated Lazarus and that issue may not exists in above 2.0.4 I am using. It is not a bug. This is by design (and also the case in Delphi). This becomes especially apparent when you do the following: 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 tintf; {$mode objfpc}{$H+} type  IIntf = interface  ['{FF53A18B-9D3B-4E6D-B597-AB78DF89F878}']    procedure Test;  end;   { TImpl }   TImpl = class(TInterfacedObject, IIntf)    procedure Test;  end;   { TProvider }   TProvider = class(TObject, IIntf)  private    fIntf: IIntf;  public    constructor Create;    property Intf: IIntf read fIntf implements IIntf;  end; { TImpl } procedure TImpl.Test;begin  Writeln('Test');end; { TProvider } constructor TProvider.Create;begin  fIntf := TImpl.Create;end; var  p: TProvider;  i: IIntf;begin  p := TProvider.Create;  // will not compile, because TProvider does not have a Test method  //p.Test;  i := p as IIntf;  i.Test;end. It would make no sense for p.Test to compile here, because TProvider does simply not have a Test method. The analogous is true for classes that directly implement the interface as the visibility of a method is dictated by the class, and not the presence of a interface.