Re: [Lazarus] operator overloading
FPC operator overloading doesn't work inside classes, it must be declared globally, so method2 should work. What error do you get? What FPC version do you use? -- View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-operator-overloading-tp4029399p4029400.html Sent from the Free Pascal - Lazarus mailing list archive at Nabble.com. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
From: xrfang Sent: Sunday, February 24, 2013 7:31 AM To: Lazarus@lists.lazarus.freepascal.org Subject: [Lazarus] operator overloading operator (p1, p2: TPainter) b: Boolean; —method2 Close, but no cigar... operator (p1, p2: TPainter): Boolean; -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
On 24.02.2013 15:51, Wildfire wrote: *From:* xrfang mailto:xrf...@gmail.com *Sent:* Sunday, February 24, 2013 7:31 AM *To:* Lazarus@lists.lazarus.freepascal.org mailto:Lazarus@lists.lazarus.freepascal.org *Subject:* [Lazarus] operator overloading operator (p1, p2: TPainter) b: Boolean; —method2 Close, but no cigar... operator (p1, p2: TPainter): Boolean; Both variants will work. The variant with b: Boolean was originally introduced for the non-Object-Pascal modes (fpc, tp), because there you only have the name of the procedure/function as a result variable and as operators don't have names (in that sense) you can specify the name of the result variable. It works in the Object Pascal modes (delphi, objfpc) however as well. Regards, Sven -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
I will test again tomorrow to find out what's wrong with my code. For now, one more queston: I noticed that generics does not work without {$mode objfpc}. I wonder what is the default mode without $mode? Why objfpc is not the default mode? 在 日, 2月 24, 2013 at 11:00 下午,Sven Barth pascaldra...@googlemail.com 写道: On 24.02.2013 15:51, Wildfire wrote: *From:* xrfang *Sent:* Sunday, February 24, 2013 7:31 AM *To:* Lazarus@lists.lazarus.freepascal.org *Subject:* [Lazarus] operator overloading operator (p1, p2: TPainter) b: Boolean; —method2 Close, but no cigar... operator (p1, p2: TPainter): Boolean; Both variants will work. The variant with b: Boolean was originally introduced for the non-Object-Pascal modes (fpc, tp), because there you only have the name of the procedure/function as a result variable and as operators don't have names (in that sense) you can specify the name of the result variable. It works in the Object Pascal modes (delphi, objfpc) however as well. Regards, Sven -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
On 24.02.2013 08:31, xrfang wrote: Hi All, How can I use operator overloading? I know the syntax, but it simply does not compile... My situation is: 1. I wrote a TTreap generic class, which is a binary tree. 2. TTreap defines a Compare() with involves and operation on its keys. 3. I try to specialize it with the following: === interface TPainterManager = class(specialize TTreapTPainter, Integer) public class operator (p1, p2: TPainter) b: Boolean; -- method1 end; operator (p1, p2: TPainter) b: Boolean; -- method2 implementation ... === but neither method1 nor method2 compiles. What is the correct way to write operator overload routines? There is only one way how you can solve this: the type TPainter MUST be a record and there you can define the operator using class operator and class operator (in mode ObjFPC you need to use {$modeswitch advancedrecords} as well). Maybe one can think about allowing operators in other structured types (classes, objects, interfaces) as well... Regards, Sven -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
Both variants will work. The variant with b: Boolean was originally introduced for the non-Object-Pascal modes (fpc, tp), because there you only have the name of the procedure/function as a result variable and as operators don't have names (in that sense) you can specify the name of the result variable. It works in the Object Pascal modes (delphi, objfpc) however as well. Thanks for the Heads Up Sven, you learn something new everyday. My apologies for the misinformation (and HTML) earlier.-- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
On 24.02.2013 16:02, xrfang wrote: I will test again tomorrow to find out what's wrong with my code. For now, one more queston: I noticed that generics does not work without {$mode objfpc}. I wonder what is the default mode without $mode? Why objfpc is not the default mode? The default mode is fpc and it will stay the default mode because of backwards compatibility. The difference between the two modes is that fpc is a bit more like tp (no classes, only objects) but with additional features. Generics will work for objects, records, arrays and procedural types in that mode. Regards, Sven -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
I confirm that it does not work. Test program below, and the class source is attached. ==Test Program=== program test; {$mode objfpc}{$H+} uses Classes, treap; type TSLCounter = specialize TTreapTStringList, Integer; operator (sl1, sl2: TStringList): Boolean; begin Result := sl1.Text sl2.Text; end; begin end. ==Error Message== Options changed, recompiling clean with -B /home/xrfang/git/fpcollection/src/units/treap.pas(216,10) Error: Operator is not overloaded: TStringList TStringList /home/xrfang/git/fpcollection/src/units/treap.pas(218,15) Error: Operator is not overloaded: TStringList TStringList test.lpr(19) Fatal: There were 2 errors compiling module, stopping = i.e. whether the operator overloading is defined or not, error message is same. Thanks. 在 日, 2月 24, 2013 at 4:21 下午,leledumbo leledumbo_c...@yahoo.co.id 写道: FPC operator overloading doesn't work inside classes, it must be declared globally, so method2 should work. What error do you get? What FPC version do you use? -- View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-operator-overloading-tp4029399p4029400.html Sent from the Free Pascal - Lazarus mailing list archive at Nabble.com. -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus unit treap; {$mode objfpc} interface type { TTreap } generic TTreapTKey, TValue = class const MAX_PRIORITY = $7FFF; type PNode = ^TNode; TNode = record Key: TKey; Value: TValue; Priority: Cardinal; Left, Right: PNode; end; private Altered: Boolean; //for Insert Delete GoOn: Boolean;//for Traverse FCount: Integer; NullNode, RootNode: PNode; function GetCount: Integer; function LeftRotate(Node: PNode): PNode; function RightRotate(Node: PNode): PNode; function InsertNode(Key: TKey; Value: TValue; Node: PNode): PNode; function DeleteNode(Key: TKey; Node: PNode): PNode; procedure TraverseNode(Node: PNode; dir: Integer); procedure ClearNode(Node: PNode); function Compare({%H-}Key: TKey; Node: PNode): Integer; protected function Traverse({%H-}Key: TKey; {%H-}Value: TValue): Boolean; virtual; procedure OnDispose({%H-}Value: TValue); virtual; function OnInsert({%H-}Key: TKey; {%H-}Value: TValue; {%H-}IsNew: Boolean): Boolean; virtual; public property Count: Integer read GetCount; function Insert(Key: TKey; Value: TValue): Boolean; function Delete(Key: TKey): Boolean; function Find(Key: TKey): PNode; procedure Clear; constructor Create; virtual; destructor Destroy; override; procedure Walk(dir: Integer = 0); //0=ascending; 1=descending end; implementation function TTreap.InsertNode(Key: TKey; Value: TValue; Node: PNode): PNode; begin if Node = NullNode then begin if OnInsert(Key, Value, True) then begin New(Node); Node^.Key := Key; Node^.Value := Value; Node^.Priority := Random(MAX_PRIORITY); Node^.Left := NullNode; Node^.Right := NullNode; Altered := True; Inc(FCount); end; end else begin case Compare(Key, Node) of 0: begin Altered := Node^.Value Value; if Altered and OnInsert(Key, Value, False) then Node^.Value := Value; end; 1: begin Node^.Right := InsertNode(Key, Value, Node^.Right); if Node^.Right^.Priority Node^.Priority then Node := RightRotate(Node); end; else begin Node^.Left := InsertNode(Key, Value, Node^.Left); if Node^.Left^.Priority Node^.Priority then Node := LeftRotate(Node); end; end; end; Result := Node; end; function TTreap.DeleteNode(Key: TKey; Node: PNode): PNode; begin if Node NullNode then begin case Compare(Key, Node) of 0: begin if Node^.Left^.Priority Node^.Right^.Priority then Node := LeftRotate(Node) else Node := RightRotate(Node); if Node NullNode then Node := DeleteNode(Key, Node) else begin OnDispose(Node^.Left^.Value); Dispose(Node^.Left); Node^.Left := NullNode; Altered := True; Dec(FCount); end; end; 1: Node^.Right := DeleteNode(Key, Node^.Right); else Node^.Left := DeleteNode(Key, Node^.Left); end; end; Result := Node; end; procedure TTreap.TraverseNode(Node: PNode; dir: Integer); begin if (not GoOn) or (Node = NullNode) then Exit; if dir = 0 then begin TraverseNode(Node^.Left, dir); if GoOn then GoOn := Traverse(Node^.Key, Node^.Value); TraverseNode(Node^.Right, dir); end else begin TraverseNode(Node^.Right, dir); if GoOn then GoOn := Traverse
[Lazarus] operator overloading
Hi All, How can I use operator overloading? I know the syntax, but it simply does not compile... My situation is: 1. I wrote a TTreap generic class, which is a binary tree. 2. TTreap defines a Compare() with involves and operation on its keys. 3. I try to specialize it with the following: === interface TPainterManager = class(specialize TTreapTPainter, Integer) public class operator (p1, p2: TPainter) b: Boolean; -- method1 end; operator (p1, p2: TPainter) b: Boolean; -- method2 implementation .. === but neither method1 nor method2 compiles. What is the correct way to write operator overload routines? Thanks, Shannon I -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
[Lazarus] operator overloading
Hi, In Delphi you can overload the Explicit operator. This makes is possible to have a type TTypeX and TTypeY and cast one into the other. To do that, implement an operator overloader: operator Explicit(const AValue: TTypeX): TTypeY. // implement converstio from TTypeX to TTypeY. Then you can do stuff like var tx: TTypeX; ty: TTypeY; begin tx := TTypeX.Create; ty := TTypeY(tx); end; Is something like this possible in FPC? Kind regards, Birger -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
2010/10/22 Birger Jansen bir...@cnoc.nl: Hi, In Delphi you can overload the Explicit operator. This makes is possible to have a type TTypeX and TTypeY and cast one into the other. To do that, implement an operator overloader: operator Explicit(const AValue: TTypeX): TTypeY. // implement converstio from TTypeX to TTypeY. Then you can do stuff like var tx: TTypeX; ty: TTypeY; begin tx := TTypeX.Create; ty := TTypeY(tx); end; Is something like this possible in FPC? I don't know. If you don't get an answer, it may be useful to ask on the fpc-pascal mailing list, that is a better place for such questions: http://lists.freepascal.org/mailman/listinfo/fpc-pascal/ Vincent -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Re: [Lazarus] operator overloading
overload the := operator instead. Search for the free pascal docs on operator overloading. -- Felipe Monteiro de Carvalho -- ___ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus