CreateTypeInstance[type,arg1,arg2,…]
在编译的代码中创建一个类型的实例.
CreateTypeInstance[productType,<|field1x1,field2x2,…|>]
创建产品类型的实例并对其字段进行初始化.
CreateTypeInstance
CreateTypeInstance[type,arg1,arg2,…]
在编译的代码中创建一个类型的实例.
CreateTypeInstance[productType,<|field1x1,field2x2,…|>]
创建产品类型的实例并对其字段进行初始化.
更多信息
- CreateTypeInstance[type,arg1,arg2,…] 中的参数可能因类型而异,并且通常与类型本身一起记录.
- 对于许多类型,如果 arg 是 type 的实例,则 CreateTypeInstance[type,arg] 创建 arg 的副本.
- 对于可以使用 CreateTypeInstance 实例化的某些类型,它们不会自动进行内存管理,必须使用 DeleteObject 手动释放.
范例
打开所有单元 关闭所有单元基本范例 (2)
编译一个函数,该函数创建长度为 5 的 "Managed"::["CArray"::["MachineInteger"]] 对象:
cf = FunctionCompile[Function[{}, CreateTypeInstance["Managed"::["CArray"::["MachineInteger"]], 5]]]cf[]typeDec = TypeDeclaration["Product", "myProd", <|"field1" -> "MachineInteger", "field2" -> "Real64"|>];cf = FunctionCompile[{typeDec},
Function[Typed[f1, "MachineInteger"],
With[{prod = CreateTypeInstance["myProd", <|"field1" -> f1, "field2" -> 3.14|>]},
prod["field1"]]]]cf[12]范围 (2)
CreateTypeInstance 的自定义用法可以使用 FunctionDeclaration 声明:
tyDec = TypeDeclaration["Product", "myProd", <|"field1" -> "MachineInteger", "field2" -> "Real64"|>];funcDec = FunctionDeclaration[CreateTypeInstance,
Typed[{"TypeSpecifier"::["myProd"], "MachineInteger"} -> "myProd"]@
Function[{ty, arg},
CreateTypeInstance["myProd", <|"field1" -> arg, "field2" -> 3.14|>]
]
];cf = FunctionCompile[{tyDec, funcDec},
Function[Typed[f1, "MachineInteger"],
With[{prod = CreateTypeInstance["myProd", f1]},
prod["field1"]]]
]cf[55]可以创建一个非托管对象,如 "CString",并且需要手动释放结果:
cf = FunctionCompile[
Function[Typed[len, "MachineInteger"],
Module[{str},
str = CreateTypeInstance["CString", len];
DeleteObject[str]
]]]cf[12]技术笔记
文本
Wolfram Research (2022),CreateTypeInstance,Wolfram 语言函数,https://reference.wolfram.com/language/ref/CreateTypeInstance.html.
CMS
Wolfram 语言. 2022. "CreateTypeInstance." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/CreateTypeInstance.html.
APA
Wolfram 语言. (2022). CreateTypeInstance. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/CreateTypeInstance.html 年
BibTeX
@misc{reference.wolfram_2026_createtypeinstance, author="Wolfram Research", title="{CreateTypeInstance}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/CreateTypeInstance.html}", note=[Accessed: 10-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_createtypeinstance, organization={Wolfram Research}, title={CreateTypeInstance}, year={2022}, url={https://reference.wolfram.com/language/ref/CreateTypeInstance.html}, note=[Accessed: 10-September-2026]}