freepascal - 带有 objfpc 模式记录的 TFPGList
问题描述
我想使用带有自定义记录的 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 模式下编写这个程序,而是在 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;
..
推荐阅读
- javascript - discord.js 中只有一个用户的命令
- reactjs - 如何使用 react-i18next
显示数组元素的组件? - sql - 用于有条件地将数据从两个表移动到另一个表的 SQL 查询
- javascript - React-native 不会更新值
- java - 通过 cms 签署数据
- c++ - 我的 Makefile 到 AVR 项目没有编译程序
- php - 无法获得我使用 GuzzleHttp\Psr7\Response 根据客户端发出的请求发送的响应
- python - How to find the Font Size of every paragraph of PDF file using python code?
- typescript - 使用 svg 文件时,“JSX 元素类型‘相机’没有任何构造或调用签名”
- node.js - 如果用户已登录,如何将用户重定向到页面