Sign in Join

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

Mailing lists / fpc-pascal / 2020-02

Interface type error

7 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Ryan Joseph

Why doesn't this compile? IClassName2 descends from IClassName1 so shouldn't TClassName be compatible with IClassName1?

========================================

{$mode objfpc}
{$interfaces corba}
program test;
type
  IClassName1 = interface
  end;
  IClassName2 = interface(IClassName1)
  end;
type
  TClassName = class(IClassName2)
  end;
procedure Pass(int: IClassName1);
begin
end;
var
  c: TClassName;
begin
  // Incompatible type for arg no. 1: Got "TClassName", expected "IClassName1"
  Pass(c);
end.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph

Ryan Joseph via fpc-pascal schrieb am
So., 2. Feb. 2020, 03:37:

Show quoted text 33 lines
Why doesn't this compile? IClassName2 descends from IClassName1 so shouldn't TClassName be compatible with IClassName1? ======================================== {$mode objfpc} {$interfaces corba} program test; type IClassName1 = interface end; IClassName2 = interface(IClassName1) end; type TClassName = class(IClassName2) end; procedure Pass(int: IClassName1); begin end; var c: TClassName; begin // Incompatible type for arg no. 1: Got "TClassName", expected "IClassName1" Pass(c); end.

As I had explained in the other thread some months ago interfaces in Object Pascal mean literally that the type can be cast to the specified interfaces. Parent interfaces are *not* part of this. If you want this you need to first cast the class to IClassName2, then the "interface inheritance" mechanism can take over.

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
On Feb 2, 2020, at 3:23 PM, Sven Barth via fpc-pascal wrote: As I had explained in the other thread some months ago interfaces in Object Pascal mean literally that the type can be cast to the specified interfaces. Parent interfaces are *not* part of this. If you want this you need to first cast the class to IClassName2, then the "interface inheritance" mechanism can take over.

Hmm, isn't that inconsistent given how classes work? if TClassName implements IClassName2 then doesn't it also implement IClassName1 by definition?

Regards,
Ryan Joseph

Reply by email

Mgr. Janusz Chmiel
to Ryan Joseph

Dear specialists here,
How complex would be to recode Free Pascal compiler for ARM and ARM64 Bit so it would be fully compatible with Android Bionic library, so it would be executed as A external app from some Android app which have GUI?

Sure. There is even Termux which can run many Linux distros by using Proot. But it would be really very very useful, if it would be possible to recode PPCJVM so atleast this compiler would work directly on Android ARM and ARM64 devices by using Android OS without complex software modules such as whole Termux, Proot and some Linux distribution.

Reply by email

Sven Barth
to Ryan Joseph: Hmm, isn't that inconsistent given how classes work? if TClassName implements IClassName2…
Ryan Joseph wrote 6 lines
> On Feb 2, 2020, at 3:23 PM, Sven Barth via fpc-pascal wrote: > > As I had explained in the other thread some months ago interfaces in Object Pascal mean literally that the type can be cast to the specified interfaces. Parent interfaces are *not* part of this. If you want this you need to first cast the class to IClassName2, then the "interface inheritance" mechanism can take over. > Hmm, isn't that inconsistent given how classes work? if TClassName implements IClassName2 then doesn't it also implement IClassName1 by definition?

You mix inheritance with interface implementation. They are conceptully *not* the same even if they use a similar syntax. Thus they can (and do) behave differently. And that means that you can cast a class instance only if that interface is explicitely implemented.

Look at the following code:

=== code begin ===

program tintftest;
{$mode objfpc}
{$interfaces corba}
type
   ITest1 = interface
     procedure Test;
   end;
   ITest2 = interface(ITest1)
     procedure Test2;
   end;
   TTest = class(TObject, ITest1, ITest2)
   public
     procedure ITest1.Test = ITest1Test;
     procedure ITest1Test;
     procedure Test;
     procedure Test2;
   end;
procedure TTest.Test;
begin
   Writeln('Test');
end;
procedure TTest.Test2;
begin
   Writeln('Test2');
end;
procedure TTest.ITest1Test;
begin
   Writeln('ITest1Test');
end;
var
   t: TTest;
   i1: ITest1;
   i2: ITest2;
begin
   t := TTest.Create;
   i1 := t;
   i1.Test;
   i2 := t;
   i2.Test;
   i2.Test2;
end.

=== code end ===

This will print:

=== output begin ===

ITest1Test
Test
Test2

=== output end ===

A class is in no way obliged to implement a interface in the same way as an descendant interface.

This might appear like an obstacle in some case but in the contrary declaring it explicitely allows for much more flexibility. Especially when you're dealing with more complex interface hierarchies.

Regards,
Sven

Reply by email

Sven Barth
to Mgr. Janusz Chmiel: How complex would be to recode Free Pascal compiler for ARM and ARM64 Bit so it would be…
Mgr. Janusz Chmiel wrote 10 lines
Dear specialists here, How complex would be to recode Free Pascal compiler for ARM and ARM64 Bit so it would be fully compatible with Android Bionic library, so it would be executed as A external app from some Android app which have GUI? Sure. There is even Termux which can run many Linux distros by using Proot. But it would be really very very useful, if it would be possible to recode PPCJVM so atleast this compiler would work directly on Android ARM and ARM64 devices by using Android OS without complex software modules such as whole Termux, Proot and some Linux distribution.

Please don't hijack existing threads by replying to an existing message with a completely new, unrelated topic. This is not only impolite to the thread participants but it also lowers the chance for your thread to be read as users might ignore messages in threads they're not interested in. Same with your message regarding Dex handling.

That said FPC already fully supports compiling native applications and libraries for Android on ARM, Aarch64, i386 and x86_64 (see here: https://wiki.freepascal.org/Android).

Regards,
Sven

Reply by email

Mgr. Janusz Chmiel
to Sven Barth

I Am very sorry. I have thought, that when I only change The subject of The message, so that it will start new thread. I will be careful on this. So instead pressinf reply to all on other thread, I will always type A new message. I Am very sorry, some times I forget on The mailing list techniques and some time I do error.

Reply by email