Recent

Author Topic: [Solved]Generic methods in interfaces - No go?!?  (Read 330 times)

cdbc

  • Hero Member
  • *****
  • Posts: 1966
    • http://www.cdbc.dk
[Solved]Generic methods in interfaces - No go?!?
« on: February 12, 2025, 10:16:56 am »
Hi
I'm testing generic methods, they work fine.
So I wanna put them in an 'interface', but that sent the compiler running and screaming... "Interfaces can't have fields!" - Hmmm, what's that all about, it's a method?!?
Code: Pascal  [Select][+][-]
  1.  {
  2.   IgenFactory = interface(IInterface)['{237FE756-12DE-4D6E-86C3-CA487F32BCB5}']
  3.    generic function PickSvc<T>(const aName: string): T;
  4.  end;                                                         }
  5.  { TgenFactory }
  6.  TgenFactory = class(TInterfacedObject{,IgenFactory})
  7.  public
  8.    generic function PickSvc<T>(const aName: string): T;
  9.  end;
The implementing class works fine, no worries, but if I uncomment the interface, the compiler instantly complains "Interfaces can't have fields!".
?!? Me No Understand ?!?  %)
Regards Benny
« Last Edit: February 12, 2025, 11:49:20 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10921
  • Debugger - SynEdit - and more
    • wiki
Re: Generic methods in interfaces - No go?!?
« Reply #1 on: February 12, 2025, 10:42:09 am »
I guess its the wrong error message...

Starting at
Code: Pascal  [Select][+][-]
  1.   TFoo = class
  2.     generic procedure Bar<T>; virtual;
  3.   end;
  4.  
gives: project1.lpr(8,38) Error: Generic methods can not be virtual

And it makes sense.

1) Virtual methods must be in the VMT.
2) The size and order of the VMT must be known (per class) and not change.

The 2nd means, imagine
- The VMT has size=10 in the unit that defines that class
- another unit_X adds a specialization at position 11
- another unit_Y also adds a specialization, it does not know about unit_X, so also  at position 11.
- Or an inherited class already has put its virtual method at position 11
...
Then if code tries to use this method, only one can be at pos 11.


Methods of interfaces are handled like virtual methods. they are listed in a table and accessed by position.
So if my understanding is right, they can not (as in: are technically not possible to) be generic.

Just the error message seems an odd choice....

Thaddy

  • Hero Member
  • *****
  • Posts: 16655
  • Kallstadt seems a good place to evict Trump to.
Re: Generic methods in interfaces - No go?!?
« Reply #2 on: February 12, 2025, 11:31:00 am »
Yes, you are right.
Interfaces can not be kamelions. They must be fully defined.
But I am sure they don't want the Trumps back...

cdbc

  • Hero Member
  • *****
  • Posts: 1966
    • http://www.cdbc.dk
Re: Generic methods in interfaces - No go?!?
« Reply #3 on: February 12, 2025, 11:47:01 am »
Hi
Thank you, the both of you  :)
Now that's cleared up, I can move on to "the next live picture", so to speak  :D
Makes perfect sense.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018