CompilerCallback
CompilerCallback["name"]
是一个函数,如果在编译器环境中定义,则由 Wolfram 编译器自动调用.
更多信息
- 支持具有指定类型的以下回调:
-
"OnFree" {t}->"Null" 在即将被释放时调用类型为 t 的内存管理对象 "SerializeToExpression" {t}->"InertExpression" 在编译代码和解释代码之间进行交互时调用,以将类型 t 的对象转换为表达式 "DeserializeFromExpression" {"InertExpression"}->t 在编译代码和解释代码之间进行交互时调用,以将表达式转换为类型为 t 的对象 "DeserializableFromExpressionQ" {"InertExpression","TypeSpecifier"::[t]}->"Boolean" 调用以检查表达式的格式是否采用可以反序列化为类型为 t 的对象 - CompilerCallback["OnFree"] 可以定义自定义解构函数,用于需要手动解构的内存管理类型.
- 在编译代码和解释代码之间进行接口时,可以定义表达式序列化回调来指定如何将类型转换为表达式,以及如何从表达式转换为类型.
范例
打开所有单元 关闭所有单元基本范例 (1)
decs = {
TypeDeclaration["Product", "myProduct", <||>],
FunctionDeclaration[CompilerCallback["OnFree"],
Typed[{"myProduct"} -> "Null"]@
Function[prod, Echo["Object freed!"];]]
};cf = FunctionCompile[decs, Function[{}, CreateTypeInstance["myProduct", <||>];]]cf[]范围 (1)
CompilerCallback 可用于指定编译类型如何与表达式相互转换. 定义产品类型:
tyDec = TypeDeclaration["Product", "myProduct", <|"f1" -> "Real64", "f2" -> "Integer64"|>];定义一个回调函数将 myProduct 对象转换为 f[{f1},f2] 形式的表达式:
serializeDec = FunctionDeclaration[CompilerCallback["SerializeToExpression"],
Typed[{"myProduct"} -> "InertExpression"]@
Function[prod,
Construct[InertExpression[f],
Construct[InertExpression[List],
Cast[prod["f1"], "InertExpression"]
],
Cast[prod["f2"], "InertExpression"]
]
]
];定义一个回调函数,来识别表达式何时以正确的形式转换为 myProduct 对象:
deserializableDec = FunctionDeclaration[
CompilerCallback["DeserializableFromExpressionQ"],
Typed[{"InertExpression", "TypeSpecifier"::["myProduct"]} -> "Boolean"]@
Function[{expr, ty},
Head[expr] === InertExpression[f] &&
Length[expr] === 2 &&
Head[expr[[1]]] === InertExpression[List] &&
Length[expr[[1]]] === 1
]
];deserializeDec = FunctionDeclaration[
CompilerCallback["DeserializeFromExpression"],
Typed[{"InertExpression"} -> "myProduct"]@
Function[expr,
CreateTypeInstance["myProduct", <|
"f1" -> Cast[expr[[1]][[1]], "Real64"],
"f2" -> Cast[expr[[2]], "Integer64"]
|>]
]
];编译一个接受并返回 myProduct 对象的函数,将其与表达式相互转换:
cf = FunctionCompile[{tyDec, serializeDec, deserializableDec, deserializeDec}, Function[Typed[obj, "myProduct"], obj]]cf[f[{3.141}, 42]]cf[g[1, 2]]属性和关系 (1)
CompiledExpressionDeclaration 自动定义表达式序列化回调. 为 f[_Real,_Integer] 形式的表达式声明一个已编译的表达式类型:
tyDec = CompiledExpressionDeclaration[f, {"Real64", "Integer64"}];cf = FunctionCompile[tyDec, Function[Typed[obj, "f"], obj]]cf[f[3.141, 42]]cf[g[1, 2]]文本
Wolfram Research (2022),CompilerCallback,Wolfram 语言函数,https://reference.wolfram.com/language/ref/CompilerCallback.html.
CMS
Wolfram 语言. 2022. "CompilerCallback." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/CompilerCallback.html.
APA
Wolfram 语言. (2022). CompilerCallback. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/CompilerCallback.html 年
BibTeX
@misc{reference.wolfram_2026_compilercallback, author="Wolfram Research", title="{CompilerCallback}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/CompilerCallback.html}", note=[Accessed: 13-October-2026]}
BibLaTeX
@online{reference.wolfram_2026_compilercallback, organization={Wolfram Research}, title={CompilerCallback}, year={2022}, url={https://reference.wolfram.com/language/ref/CompilerCallback.html}, note=[Accessed: 13-October-2026]}