CompilerCallback

CompilerCallback["name"]

is a function that, if defined in a compiler environment, is automatically called by the Wolfram Compiler.

Details

  • The following callbacks are supported with the specified types:
  • "OnFree"{t}->"Null"called on memory-managed objects with the type t when they are about to be freed
    "SerializeToExpression"{t}->"InertExpression"called to convert objects of type t to expressions when interfacing between compiled and interpreted code
    "DeserializeFromExpression"{"InertExpression"}->tcalled to convert expressions to objects of type t when interfacing between compiled and interpreted code
    "DeserializableFromExpressionQ"{"InertExpression","TypeSpecifier"::[t]}->"Boolean"called to check whether an expression is in a form that can be deserialized as an object with type t
  • CompilerCallback["OnFree"] can be defined to specify custom deconstructors for memory-managed types that require manual deconstruction.
  • Expression serialization callbacks can be defined to specify how types are converted to and from expressions when interfacing between compiled and interpreted code.

Examples

open allclose all

Basic Examples  (1)

Declare a memory-managed type and a callback function to be executed when it is freed:

Compile a function that creates the type and then allows it to be freed:

Run the compiled function, with the callback executing when the object goes out of scope:

Scope  (1)

CompilerCallback can be used to specify how compiled types are converted to and from expressions. Define a product type:

Define a callback function to convert myProduct objects into expressions of the form f[{f1},f2]:

Define a callback function to identify when an expression is in the correct form to be converted to a myProduct object:

Define a callback function to convert an expression to a myProduct object:

Compile a function that accepts and returns a myProduct object, converting it to and from an expression:

Expressions that do not have the correct form will not be converted:

Properties & Relations  (1)

CompiledExpressionDeclaration automatically defines expression serialization callbacks. Declare a compiled expression type for expressions of the form f[_Real,_Integer]:

Compile a function that accepts and returns this type, converting it to and from an expression:

Expressions that do not have the correct form will not be converted:

Wolfram Research (2022), CompilerCallback, Wolfram Language function, https://reference.wolfram.com/language/ref/CompilerCallback.html.

Text

Wolfram Research (2022), CompilerCallback, Wolfram Language function, https://reference.wolfram.com/language/ref/CompilerCallback.html.

CMS

Wolfram Language. 2022. "CompilerCallback." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/CompilerCallback.html.

APA

Wolfram Language. (2022). CompilerCallback. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CompilerCallback.html

BibTeX

@misc{reference.wolfram_2024_compilercallback, author="Wolfram Research", title="{CompilerCallback}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/CompilerCallback.html}", note=[Accessed: 21-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_compilercallback, organization={Wolfram Research}, title={CompilerCallback}, year={2022}, url={https://reference.wolfram.com/language/ref/CompilerCallback.html}, note=[Accessed: 21-December-2024 ]}