TFPGList with record in objfpc mode
TFPGList with record in objfpc mode
我想使用带有自定义记录的 TFPGList。我花了很长时间才从互联网上获得所有必要的提示来编译这个小片段:
program Project1;
{$mode delphi}{$H+}
uses
fgl;
type
TSomeRecord = record
feld_1: Byte;
class operator Equal(Left, Right : TSomeRecord) Result : Boolean;
end;
class operator TSomeRecord.Equal (Left, Right: TSomeRecord) Result: Boolean;
begin
Result := Left.feld_1 = Right.feld_1;
end;
type
TypedList = TFPGList<TSomeRecord>;
var
x : TypedList;
begin
end.
如您所见,问题在于为记录指定 Equal 运算符。此外,这似乎仅在 delphi 模式下可行。
假设我不想在 delphi 模式下,而是在 objfpc 模式下编写这个程序:将 Equal 运算符指定给记录的正确语法是什么?可能吗?
我的fpc版本是3.0.4
(* Please try the following compiled with Lazarus 2.06, FPC 3.04: *)
unit..
..
{$IFDEF fpc} {$MODESWITCH AdvancedRecords+} {$ENDIF}
..
interface
..
type
TSomeRecord = record
feld_1: Byte;
class operator = (Left, Right : TSomeRecord): Boolean;
end;
..
implementation
..
class operator TSomeRecord .= (Left, Right : TSomeRecord): Boolean;
begin
Result:=Left.feld_1 = Right.feld_1;
end;
..
我想使用带有自定义记录的 TFPGList。我花了很长时间才从互联网上获得所有必要的提示来编译这个小片段:
program Project1;
{$mode delphi}{$H+}
uses
fgl;
type
TSomeRecord = record
feld_1: Byte;
class operator Equal(Left, Right : TSomeRecord) Result : Boolean;
end;
class operator TSomeRecord.Equal (Left, Right: TSomeRecord) Result: Boolean;
begin
Result := Left.feld_1 = Right.feld_1;
end;
type
TypedList = TFPGList<TSomeRecord>;
var
x : TypedList;
begin
end.
如您所见,问题在于为记录指定 Equal 运算符。此外,这似乎仅在 delphi 模式下可行。
假设我不想在 delphi 模式下,而是在 objfpc 模式下编写这个程序:将 Equal 运算符指定给记录的正确语法是什么?可能吗?
我的fpc版本是3.0.4
(* Please try the following compiled with Lazarus 2.06, FPC 3.04: *)
unit..
..
{$IFDEF fpc} {$MODESWITCH AdvancedRecords+} {$ENDIF}
..
interface
..
type
TSomeRecord = record
feld_1: Byte;
class operator = (Left, Right : TSomeRecord): Boolean;
end;
..
implementation
..
class operator TSomeRecord .= (Left, Right : TSomeRecord): Boolean;
begin
Result:=Left.feld_1 = Right.feld_1;
end;
..