Recent

Author Topic: [closed] Code optimization and interfaces  (Read 2743 times)

HuntingKashket

  • New Member
  • *
  • Posts: 33
  • I'm interested in upgrading everything
[closed] Code optimization and interfaces
« on: March 17, 2019, 11:09:26 am »
Hello everyone, i am writing about very strange syntax flavor - optimization of interface-inherited classes:
when you're declaring a class derived from interface with interface-addressed functions, the compiler says about unused variables, which is really just used to pass a parameters to com or other outer object, which not met in the current source, e.g typelibrary, COM, OLE - objects/interfaces

Code from CEF4Delphi extension
Unit uCefInerfaces.pas
Code: Pascal  [Select][+][-]
  1. // TCefBinaryValue
  2.   // /include/capi/cef_values_capi.h (cef_binary_value_t)
  3.   ICefBinaryValue = interface(ICefBaseRefCounted)
  4.     ['{974AA40A-9C5C-4726-81F0-9F0D46D7C5B3}']
  5.     function IsValid: Boolean;
  6.     function IsOwned: Boolean;
  7.     function IsSame(const that: ICefBinaryValue): Boolean;
  8.     function IsEqual(const that: ICefBinaryValue): Boolean;
  9.     function Copy: ICefBinaryValue;
  10.     function GetSize: NativeUInt;
  11.     function GetData(buffer: Pointer; bufferSize, dataOffset: NativeUInt): NativeUInt;
  12.  
  13.     property Size  : NativeUInt   read GetSize;
  14.   end;  
  15.  

Unit uCEFBinaryValue.pas
Code: Pascal  [Select][+][-]
  1. TCefBinaryValueOwn = class(TCefBaseRefCountedOwn, ICefBinaryValue)
  2.     protected
  3.       function IsValid: Boolean;
  4.       function IsOwned: Boolean;
  5.       function IsSame(const that: ICefBinaryValue): Boolean;
  6.       function IsEqual(const that: ICefBinaryValue): Boolean;
  7.       function Copy: ICefBinaryValue;
  8.       function GetSize: NativeUInt;
  9.       function GetData(buffer: Pointer; bufferSize, dataOffset: NativeUInt): NativeUInt;
  10.  
  11.     public
  12.       constructor Create;
  13.   end;  
  14.  

I get hints or warnings about all of the interfaced functions with parameters, like that:
 <<Hint: %s1%(%d1%,%d2%): parameter "%s2" not used>>

Is something going to change in newer versions of lazarus/fpc?
« Last Edit: March 18, 2019, 06:32:00 pm by HuntingKashket »
Leu Zenin
-------------------------------
Lazarus 2.1.0  with FPC 3.1
Windows 8.1 x64

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Code optimization and interfaces
« Reply #1 on: March 17, 2019, 11:34:07 am »
Hello everyone, i am writing about very strange syntax flavor - optimization of interface-inherited classes:
Like this?
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. type
  4.   ITestInterface = interface(IInterface)
  5.     procedure Test(SomeParam: Integer);
  6.   end;
  7.  
  8.   TTestClass = class(TInterfacedObject, ITestInterface)
  9.   private
  10.     procedure Test(SomeParam: Integer);
  11.   end;
  12.  
  13. procedure TTestClass.Test(SomeParam: Integer);
  14. begin
  15.   // If SomeParam is not used, the compiler hints at this, but it is true
  16.   // for all cases, with or without an interfaces
  17. end;
  18.  
  19. begin
  20. end.

HuntingKashket

  • New Member
  • *
  • Posts: 33
  • I'm interested in upgrading everything
Re: Code optimization and interfaces
« Reply #2 on: March 17, 2019, 12:00:35 pm »
Hello everyone, i am writing about very strange syntax flavor - optimization of interface-inherited classes:
Like this?
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. type
  4.   ITestInterface = interface(IInterface)
  5.     procedure Test(SomeParam: Integer);
  6.   end;
  7.  
  8.   TTestClass = class(TInterfacedObject, ITestInterface)
  9.   private
  10.     procedure Test(SomeParam: Integer);
  11.   end;
  12.  
  13. procedure TTestClass.Test(SomeParam: Integer);
  14. begin
  15.   // If SomeParam is not used, the compiler hints at this, but it is true
  16.   // for all cases, with or without an interfaces
  17. end;
  18.  
  19. begin
  20. end.
No, because this interface is created not to export, but to import the function functions.
The original object with all of its functions is stored in libcef.dll, which is the target of COM-interface

In delphi it's ok for compiler to use object functions from library, in lazarus this code produces hints, idk, perhaps author just didn't implemented some interfaces for lazarus, so i questioned about that.
« Last Edit: March 17, 2019, 12:13:25 pm by HuntingKashket »
Leu Zenin
-------------------------------
Lazarus 2.1.0  with FPC 3.1
Windows 8.1 x64

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Code optimization and interfaces
« Reply #3 on: March 18, 2019, 09:25:08 am »
No, because this interface is created not to export, but to import the function functions.
The original object with all of its functions is stored in libcef.dll, which is the target of COM-interface

But then you don't need TCefBinaryValueOwn. You only need a class implementing an interface if you want to use that class to provide that interface. If you merely import then you don't need a class for that.

In delphi it's ok for compiler to use object functions from library, in lazarus this code produces hints, idk, perhaps author just didn't implemented some interfaces for lazarus, so i questioned about that.

I think Delphi does not have a hint/note for "parameter not used" at all. But FPC does and so it provides that accordingly for the methods of the class.

HuntingKashket

  • New Member
  • *
  • Posts: 33
  • I'm interested in upgrading everything
Re: Code optimization and interfaces
« Reply #4 on: March 18, 2019, 09:55:14 am »
No, because this interface is created not to export, but to import the function functions.
The original object with all of its functions is stored in libcef.dll, which is the target of COM-interface

But then you don't need TCefBinaryValueOwn. You only need a class implementing an interface if you want to use that class to provide that interface. If you merely import then you don't need a class for that.

In delphi it's ok for compiler to use object functions from library, in lazarus this code produces hints, idk, perhaps author just didn't implemented some interfaces for lazarus, so i questioned about that.

I think Delphi does not have a hint/note for "parameter not used" at all. But FPC does and so it provides that accordingly for the methods of the class.
Delphi had it, otherwise i wouldn't mention that, so i am quite confused about warning, and i stay assume that it's just an fpc thing, which haven't been included in the wiki.

UPD: maybe dcc is poor in interfaces analyzing, i'm not sure about.
« Last Edit: March 18, 2019, 10:55:30 am by HuntingKashket »
Leu Zenin
-------------------------------
Lazarus 2.1.0  with FPC 3.1
Windows 8.1 x64

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Code optimization and interfaces
« Reply #5 on: March 18, 2019, 03:55:21 pm »
No, because this interface is created not to export, but to import the function functions.
Ok, this import test. No warning about unused variables.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. type
  4.   ITestInterface = interface(IInterface)
  5.     procedure Test(SomeParam: Integer);
  6.   end;
  7.  
  8. function ExternalFunction: ITestInterface;
  9. begin
  10.   Result := nil;
  11. end;
  12.  
  13. begin
  14.   ExternalFunction;
  15. end.

HuntingKashket

  • New Member
  • *
  • Posts: 33
  • I'm interested in upgrading everything
Re: Code optimization and interfaces
« Reply #6 on: March 18, 2019, 06:29:45 pm »
No, because this interface is created not to export, but to import the function functions.
Ok, this import test. No warning about unused variables.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. type
  4.   ITestInterface = interface(IInterface)
  5.     procedure Test(SomeParam: Integer);
  6.   end;
  7.  
  8. function ExternalFunction: ITestInterface;
  9. begin
  10.   Result := nil;
  11. end;
  12.  
  13. begin
  14.   ExternalFunction;
  15. end.
Ok, sorry, you are right, i've found implementation of it, the way it's attached is very strange,,, pointer+interface. some very unusual trash solution. I wonder, why delphi 10.1 says nothing about that, when d10.1 update 2 outputs warning like fpc/lazarus?
[upd] delphi is strange, very very very strange, it's better to have no experience how to deal with it.
« Last Edit: March 18, 2019, 06:35:35 pm by HuntingKashket »
Leu Zenin
-------------------------------
Lazarus 2.1.0  with FPC 3.1
Windows 8.1 x64

 

TinyPortal © 2005-2018