跳到主要内容

泛型

zts.make_generic_type 得到 闭合 泛型类型对象,再像普通类型一样构造与调成员。数组见 数组。权威:05-LIB类型系统

开放泛型定义本身 不可 直接 new 实例化。

闭合泛型类型

未闭合定义须带 反引号 arityList`1Dictionary`2

const ListInt = zts.make_generic_type(
CSharp.mscorlib['System.Collections.Generic.List`1'],
zts.types.int32
);

const list = new ListInt();
list.Add(10);
list.Add(20);
const DictStrInt = zts.make_generic_type(
CSharp.mscorlib['System.Collections.Generic.Dictionary`2'],
zts.types.string,
zts.types.int32
);

const dict = new DictStrInt();
dict.Add("hp", 100);

亦可配合 csharp: 模块:

import { List } from "csharp:mscorlib/System.Collections.Generic";

const ListInt = zts.make_generic_type(List, zts.types.int32);
const list = new ListInt();

第一个参数为 泛型定义类型对象,其后为类型实参(zts.types.*、类型对象、名称字符串等,见 typeArg)。相同实参 intern 同一类型对象。

也可用 AQN 一次解析闭合类型:

const ListInt2 = zts.get_type_from_name(
"System.Collections.Generic.List`1[[System.Int32, mscorlib]]"
);

泛型方法

方法 自身 带类型参数(如开放 Foo<T>)时,用 zts.make_generic_method 得到单态化 direct function

const bar_int = zts.make_generic_method(MyType.GenericBar, zts.types.int32);
bar_int(obj, 42); // 注意:提取后的调用须自行保证 this 约定;优先保持方法调用形态
规则说明
第一参类型对象上的 direct method function
传入 dispatchthrow
已闭合类上的普通方法(如 List<int>.Add走此路径

类型系统05-LIB §9

常见错误

现象处理
arity 错误检查 `1 / `2 与实参个数
List 找不到使用 System.Collections.Generic.List1`` 全名 + 括号键
对开放定义直接 newmake_generic_type
对 dispatch 调 make_generic_method先消歧为 direct(全签名键 / 单候选)

相关文档