Recent

Author Topic: (Overload) Generic procedure  (Read 1043 times)

trexet

  • New Member
  • *
  • Posts: 37
(Overload) Generic procedure
« on: June 19, 2024, 07:26:48 pm »
Hi again. I'm trying to make a generic procedure, which is overloaded using generic class:
Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3.     {...}
  4.  
  5.     generic procedure InitLC<T: TEntity>(LC: specialize TLC_EL_Connector<T>;
  6.                                          OwnerBox: TGroupBox;
  7.                                          LCCActions: TLCCActions); overload;
  8.     generic procedure InitLC<T: TEntity>(LC: specialize TLC_IDL_Connector<T>;
  9.                                          OwnerBox: TGroupBox;
  10.                                          LCCActions: TLCCActions); overload;
  11.  
  12. implementation
  13.  
  14. generic procedure InitLC<T: TEntity>(LC: specialize TLC_EL_Connector<T>;
  15.                                      OwnerBox: TGroupBox;
  16.                                      LCCActions: TLCCActions);
  17. begin
  18.  
  19. end;
  20.  
  21. generic procedure InitLC<T: TEntity>(LC: specialize TLC_IDL_Connector<T>;
  22.                                      OwnerBox: TGroupBox;
  23.                                      LCCActions: TLCCActions);
  24. begin
  25.  
  26. end;

However,
Quote
editor.pas(70,23) Error: Forward declaration not solved "InitLC$1(TLC_EL_Connector$1$crcFB7CCBD1;TGroupBox;TLCCActions);"
editor.pas(73,23) Error: Forward declaration not solved "InitLC$1(TLC_IDL_Connector$1$crcD479001B;TGroupBox;TLCCActions);"

UPD: Seems that is has nothing to do with overload. Declaring the procedures as InitLC1 and InitLC2 makes the same error.

FPC 3.2.2, Lazarus 3.4
« Last Edit: June 19, 2024, 07:39:31 pm by trexet »

Thaddy

  • Hero Member
  • *****
  • Posts: 16194
  • Censorship about opinions does not belong here.
Re: (Overload) Generic procedure
« Reply #1 on: June 19, 2024, 07:55:03 pm »
You can not overload generics, because they are generic.
This is also not possible in languages like C++.
If I smell bad code it usually is bad code and that includes my own code.

trexet

  • New Member
  • *
  • Posts: 37
Re: (Overload) Generic procedure
« Reply #2 on: June 19, 2024, 08:13:50 pm »
Quote
You can not overload generics
No, I can. Tested it around and found the following works (mode delphi, with mode objfpc and correct syntax works as well):

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. procedure InitLC<T: TEntity>(var LC: TLC_EL_Connector<T>;
  4.     constref OwnerBox: TGroupBox; constref LCCActions: TLCCActions); overload;
  5. begin
  6.     {...}
  7. end;
  8.  
  9. procedure InitLC<T: TEntity>(var LC: TLC_IDL_Connector<T>;
  10.     constref OwnerBox: TGroupBox; constref LCCActions: TLCCActions); overload;
  11. begin
  12.     {...}
  13. end;

Although, declaring the procedures in the interface won't work. Good thing I didn't need that.

Quote
This is also not possible in languages like C++.
Absolutely possible:
Code: C  [Select][+][-]
  1. template<typename T>
  2. T f(int x, T y);
  3.  
  4. template<typename T>
  5. T f(double x, T y);
  6.  
  7. int main()
  8. {
  9.     std::printf("f<int>(int) %d\nf<double>(int) %f\nf<int>(double) %d\nf<double>(double) %f\n",
  10.                 f<int>(1, 1),
  11.                 f<double>(1, 1.0),
  12.                 f<int>(1.0, 1),
  13.                 f<double>(1.0, 1.0));
  14.  
  15.     return 0;
  16. }
  17.  
  18. template<typename T>
  19. T f(int x, T y) {
  20.     return x + y;
  21. }
  22.  
  23. template<typename T>
  24. T f(double x, T y) {
  25.     return x - y;
  26. }

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: (Overload) Generic procedure
« Reply #3 on: June 23, 2024, 05:07:25 pm »
Hi again. I'm trying to make a generic procedure, which is overloaded using generic class:

The problem is that constraints currently aren't handled correctly to correctly match the implementation to the declaration. It's on my todo-list, but it's a bit involved solve...

As a workaround you could make these functions instead static non-generic methods of a generic record which can then have the constraint.

 

TinyPortal © 2005-2018