* * *

Author Topic: Why I received "Operator is not overloaded" complie error while using Generic?  (Read 1147 times)

shyakocat

  • Newbie
  • Posts: 2
I feel confused when I'm programming such kind of code.
I use Free Pascal 3.0.2;

Code: Pascal  [Select]
  1. Uses GUtil;
  2. Type
  3.  Rana=Record x,y:Longint End;
  4.  
  5. Operator <(const a,b:Rana)c:Boolean;  //hint:const or not doesn't matter
  6. Begin Exit(a.x<b.x) End;
  7.  
  8. Type
  9.  RLess=Specialize TLess<Rana>;
  10.  
  11. Begin
  12.  
  13. End.
  14.  

If I complie codes above.I'll got an {Error: Operator is not overloaded: "Rana" < "Rana" }.Why is that and what should I do to solve this.
« Last Edit: November 10, 2017, 04:47:42 am by shyakocat »

molly

  • Hero Member
  • *****
  • Posts: 2343
This contruct is not possible yet, see also operator overloading.
Quote
Currently, due to an implementation restriction, it will not work using a global operator..
and
Quote
Support for this construct is expected in a future version of Free Pascal.

Use advanced record with a record operator:
Code: Pascal  [Select]
  1. {$MODESWITCH ADVANCEDRECORDS}
  2.  
  3. Uses
  4.   GUtil;
  5.  
  6. Type
  7.   Rana=
  8.   Record
  9.     x,y:Longint;
  10.     class Operator <(const a,b:Rana)c:Boolean;  //hint:const or not doesn't matter
  11.   End;
  12.  
  13. class Operator rana.<(const a,b:Rana)c:Boolean;  //hint:const or not doesn't matter
  14. Begin
  15.   Exit(a.x<b.x)
  16. End;
  17.  
  18. Type
  19.  RLess=Specialize TLess<Rana>;
  20.  
  21. Begin
  22. End.
  23.  

shyakocat

  • Newbie
  • Posts: 2
Thanks a lot.

Thaddy

  • Hero Member
  • *****
  • Posts: 5780
The construct is possible with 3.0.4 and trunk, but there is a limitation in the fgl unit. .
Here is an example using fcl-stl and another generic container type:
Code: Pascal  [Select]
  1. program untitled;
  2. {$mode objfpc}{$H+}{$I-}
  3. uses glinkedlist;//generics.collections; is also possible from trunk
  4. type
  5.   TVec2 = object
  6.   end;
  7.   TVec2List = specialize TlinkedList < TVec2 >; //generics.collections: Tlist<>
  8.  
  9. operator = (const A:Tvec2,TVec2 ): Boolean;
  10. begin
  11.   Result:= True;
  12. end;
  13. begin
  14. end.
  15.  

The language supports it since 3.0.4. for anything, records, classes, objects, simple types... Docs are outdated, I think, unless it missed the 3.0.4 release...
Anyway: with trunk even more is possible.
« Last Edit: January 03, 2018, 05:10:53 pm by Thaddy »
recommends {$macro on}{$define Silly:=ObjFpc}

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus