Recent

Author Topic: operator overloading with generics  (Read 2436 times)

airpas

  • Full Member
  • ***
  • Posts: 179
operator overloading with generics
« on: May 10, 2015, 10:05:35 am »
hi 
operator overloading needs specialization on its arguments , do i need to specialize for every type ?

Code: [Select]
{$mode objfpc}
type
generic obj<T> = object
private
i : T;
public
property pVal : T read i write i;
end;


operator + (arg1 , arg2 : obj) : obj;
begin
end;


airpas

  • Full Member
  • ***
  • Posts: 179
Re: operator overloading with generics
« Reply #1 on: May 10, 2015, 10:52:20 pm »
in delphi mode this works fine , how can write this in objfpc ?
Code: [Select]
type
     obj<T> = record
x : T;
class operator add (arg1 ,arg2 : obj<T>):obj<T>; inline;
     end;

class operator obj<T>.add (arg1 ,arg2 : obj<T>):obj<T>;
begin
result.x := arg1.x + arg2.x;
end;


Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: operator overloading with generics
« Reply #2 on: May 10, 2015, 11:07:31 pm »
Maybe:
Code: [Select]
{$modeswitch advancedrecords}
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

airpas

  • Full Member
  • ***
  • Posts: 179
Re: operator overloading with generics
« Reply #3 on: May 11, 2015, 09:11:51 am »
thanks
do you mean , i have to add {$modeswitch advancedrecords} only ! . is so  , it didn't help
seems not possible to write this code in objfpc mode ! , because in objfpc mode the operator should be outside of the class , in this case there is no way to define <T> there !
 

 

TinyPortal © 2005-2018