Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2012-04

Interface: Method resolution clauses

11 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marcos Douglas

Hi,

Can I, using FPC trunk, do this sintaxe?

type
  TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo)
     function IMalloc.Alloc = Allocate;
     procedure IMalloc.Free = Deallocate;
    ...
end;

I'm using 2.6.1 and do not works.

From Dephi's (7) help:

----------------------------------
"You can override the default name-based mappings by including method resolution clauses in a class declaration. When a class implements two or more interfaces that have identically named methods, use method resolution clauses to resolve the naming conflicts.

A method resolution clause has the form

procedure interface.interfaceMethod = implementingMethod;

or

function interface.interfaceMethod = implementingMethod;

where implementingMethod is a method declared in the class or one of its ancestors. The implementingMethod can be a method declared later in the class declaration, but cannot be a private method of an ancestor class declared in another module.

For example, the class declaration

type
  TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo)
function IMalloc.Alloc = Allocate;
procedure IMalloc.Free = Deallocate;
    ...
end;

maps IMalloc's Alloc and Free methods onto TMemoryManager's Allocate and Deallocate methods.

A method resolution clause cannot alter a mapping introduced by an ancestor class."

Thanks,

Marcos Douglas

Reply by email

Marco van de Voort
to Marcos Douglas

In our previous episode, Marcos Douglas said:

Show quoted text 8 lines
Can I, using FPC trunk, do this sintaxe? type TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo) function IMalloc.Alloc = Allocate; procedure IMalloc.Free = Deallocate; ... end;

No. See bug #21064.

p.s. In a case like this searching for "interface" in mantis and clicking a few reports there, will work faster :-)

Reply by email

Sergei Gorelkin
to Marcos Douglas: Can I, using FPC trunk, do this sintaxe? type TMemoryManager = class(TInterfacedObject,…

30.04.2012 19:06, Marcos Douglas пишет:

Show quoted text 13 lines
Hi, Can I, using FPC trunk, do this sintaxe? type TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo) function IMalloc.Alloc = Allocate; procedure IMalloc.Free = Deallocate; ... end; I'm using 2.6.1 and do not works.

To my knowledge, this kind of stuff should work.
What exact problem are you experiencing?

Sergei

Reply by email

Marcos Douglas
to Marco van de Voort: In our previous episode, Marcos Douglas said: No. See bug #21064. p.s. In a case like…
Marco van de Voort wrote 11 lines
In our previous episode, Marcos Douglas said: > Can I, using FPC trunk, do this sintaxe? > > type >   TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo) >      function IMalloc.Alloc = Allocate; >      procedure IMalloc.Free = Deallocate; >     ... > end; No. See bug #21064.

Thanks.

p.s. In a case like this searching for "interface" in mantis and clicking a few reports there, will work faster :-)

You propose me to search for a bug instead of a solution? :) Forget, I understood.

Marcos Douglas

p.s: Do you think that my work was stoped because the FPC do not have this feature?

Reply by email

Marcos Douglas
to Sergei Gorelkin: 30.04.2012 19:06, Marcos Douglas пишет: To my knowledge, this kind of stuff should…
Sergei Gorelkin wrote 19 lines
30.04.2012 19:06, Marcos Douglas пишет: > Hi, > > Can I, using FPC trunk, do this sintaxe? > > type >   TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo) >      function IMalloc.Alloc = Allocate; >      procedure IMalloc.Free = Deallocate; >     ... > end; > > I'm using 2.6.1 and do not works. > To my knowledge, this kind of stuff should work. What exact problem are you experiencing? Sergei

Simple. Does not compile.

Marcos Douglas

Reply by email

Sergei Gorelkin
to Marcos Douglas

30.04.2012 20:38, Marcos Douglas пишет:

Simple. Does not compile.

The following compiles successfully (in Win32):

---------------------------------------------- cut
{$mode objfpc}{$h+}
uses activex;
type
   TMemoryManager = class(TInterfacedObject, IMalloc)
      function IMalloc.Alloc = Allocate;
      procedure IMalloc.Free = Deallocate;
      function IMalloc.Realloc = Reallocate;
      function IMalloc.GetSize = get_size;
      function IMalloc.DidAlloc = did_alloc;
      procedure IMalloc.HeapMinimize = heap_minimize;
      function allocate(sz: longword): pointer; stdcall;
      procedure deallocate(p: pointer); stdcall;
      function reallocate(p: pointer; sz: longword): pointer; stdcall;
      function get_size(p: pointer): longword; stdcall;
      function did_alloc(p: pointer): longint; stdcall;
      procedure heap_minimize; stdcall;
   end;
function TMemoryManager.allocate(sz: longword): pointer; stdcall;
begin
end;
procedure TMemoryManager.deallocate(p: pointer); stdcall;
begin
end;
function TMemoryManager.reallocate(p: pointer; sz: longword): pointer; stdcall;
begin
end;
function TMemoryManager.get_size(p: pointer): longword; stdcall;
begin
end;
function TMemoryManager.did_alloc(p: pointer): longint; stdcall;
begin
end;
procedure TMemoryManager.heap_minimize; stdcall;
begin
end;
begin
end.
------------------------------------------- cut

Sergei

Reply by email

Marcos Douglas
to Sergei Gorelkin
Sergei Gorelkin wrote 60 lines
30.04.2012 20:38, Marcos Douglas пишет: > > > Simple. Does not compile. > The following compiles successfully (in Win32): ---------------------------------------------- cut {$mode objfpc}{$h+} uses activex; type  TMemoryManager = class(TInterfacedObject, IMalloc)     function IMalloc.Alloc = Allocate;     procedure IMalloc.Free = Deallocate;     function IMalloc.Realloc = Reallocate;     function IMalloc.GetSize = get_size;     function IMalloc.DidAlloc = did_alloc;     procedure IMalloc.HeapMinimize = heap_minimize;     function allocate(sz: longword): pointer; stdcall;     procedure deallocate(p: pointer); stdcall;     function reallocate(p: pointer; sz: longword): pointer; stdcall;     function get_size(p: pointer): longword; stdcall;     function did_alloc(p: pointer): longint; stdcall;     procedure heap_minimize; stdcall;  end; function TMemoryManager.allocate(sz: longword): pointer; stdcall; begin end; procedure TMemoryManager.deallocate(p: pointer); stdcall; begin end; function TMemoryManager.reallocate(p: pointer; sz: longword): pointer; stdcall; begin end; function TMemoryManager.get_size(p: pointer): longword; stdcall; begin end; function TMemoryManager.did_alloc(p: pointer): longint; stdcall; begin end; procedure TMemoryManager.heap_minimize; stdcall; begin end; begin end. ------------------------------------------- cut Sergei

FPC 2.6.1 or 2.7.1 (trunk)?
I'm using 2.6.1 and I can't compile it.

Marcos Douglas

Reply by email

Sergei Gorelkin
to Marcos Douglas

30.04.2012 22:42, Marcos Douglas пишет:

FPC 2.6.1 or 2.7.1 (trunk)? I'm using 2.6.1 and I can't compile it.

I was testing with trunk, but I don't remember significant changes to this part of compiler after 2.6.0. What error do you get?

Sergei

Reply by email

Marcos Douglas
to Sergei Gorelkin
Sergei Gorelkin wrote 12 lines
30.04.2012 22:42, Marcos Douglas пишет: > > FPC 2.6.1 or 2.7.1 (trunk)? > I'm using 2.6.1 and I can't compile it. > I was testing with trunk, but I don't remember significant changes to this part of compiler after 2.6.0. What error do you get? Sergei

You're right, sorry.
See:

Ex:
  ImyInterface = interface
    procedure Foo;
  end;
 TmyClass = class(TInterfacedObject, ImyInterface)
 public
   procedure ImyInterface.Foo = myFoo;
   procedure myFoo;  << this line was missing
 end;

I thought it was not necessary.
Thank you.

Marcos Douglas

Reply by email

Marcos Douglas
to Marco van de Voort
Marco van de Voort wrote 6 lines
In our previous episode, Marco van de Voort said: > > end; > > No. See bug #21064. This involves generics, so is something different.

Yeah, I saw... This confused the two of us.
Thanks anyway.

Marcos Douglas

Reply by email