delphi - Delphi XE4 通用包装器中的无效类型
问题描述
TDictionary<string,T>
出于某种原因,我想在 a 周围使用包装器。for
但是当我尝试通过编译器迭代地图时说:
[dcc32 Error] Unit1.pas(23): E2010 Incompatible types: 'T' and 'System.Generics.Collections.TPair<System.string,Unit1.TMyMapWrapper<T>.T>'
如何修改泛型类型声明以使这样的简单代码可编译?
这是我的简化代码:
unit Unit1;
interface
implementation
uses
Generics.Collections
;
type
TMyMapWrapper<T> = class
private
fMap : TDictionary<string,T>;
public
procedure foo;
end;
procedure TMyMapWrapper<T>.foo;
var
item : T;
begin
for item in fMap do
;
end;
end.
解决方案
如果X
是类型TDictionary<A, B>
,那么枚举项将是类型TPair<A, B>
,而不是B
。
var
item: TPair<string, T>;
begin
for item in fMap do // will compile
如果您只想枚举字典的值(类型T
),请使用
var
val: T;
begin
for val in fMap.Values do // will compile
推荐阅读
- python - 为什么python不应该读取else语句中的代码
- docker - Apache Nutch 不公开其 API
- swift - 如何在 SwiftUI 中单击按钮时验证动态添加的文本字段?
- javascript - ChartsJS 图例未在 Angular11 中显示
- reactjs - 如何在 redux-persist 中仅将一部分 redux 存储持久化到本地存储中
- r - grid.rect 函数将始终覆盖上一个图像
- yolo - 使用 yolo 模型查找人类号码
- android - 如果没有 URL,有人可以访问我的 Firebase 数据库吗?
- vue.js - onRemoteStreamListener 不会在发起方(Web 应用程序)触发
- java - 通过 Fluentd 将 Spring Boot 应用程序日志记录到 Elasticsearch