CreateTypeInstance[type,arg1,arg2,…]
creates an instance of a type in compiled code.
CreateTypeInstance[productType,<|field1x1,field2x2,…|>]
creates an instance of a product type and initializes its fields.
CreateTypeInstance
CreateTypeInstance[type,arg1,arg2,…]
creates an instance of a type in compiled code.
CreateTypeInstance[productType,<|field1x1,field2x2,…|>]
creates an instance of a product type and initializes its fields.
Details
- The arguments in CreateTypeInstance[type,arg1,arg2,…] can vary from type to type and are often documented with the type itself.
- For many types, CreateTypeInstance[type,arg] creates a copy of arg if arg is an instance of type.
- Some types that can be instantiated with CreateTypeInstance are not automatically memory managed and must be manually freed with DeleteObject.
Examples
open all close allBasic Examples (2)
Compile a function that creates a "Managed"::["CArray"::["MachineInteger"]] object with length 5:
cf = FunctionCompile[Function[{}, CreateTypeInstance["Managed"::["CArray"::["MachineInteger"]], 5]]]cf[]Create a product type declaration for "myProd":
typeDec = TypeDeclaration["Product", "myProd", <|"field1" -> "MachineInteger", "field2" -> "Real64"|>];Compile a function that instantiates "myProd":
cf = FunctionCompile[{typeDec},
Function[Typed[f1, "MachineInteger"],
With[{prod = CreateTypeInstance["myProd", <|"field1" -> f1, "field2" -> 3.14|>]},
prod["field1"]]]]cf[12]Scope (2)
Custom usages of CreateTypeInstance can be declared with 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|>]
]
];Compile a function that uses the custom constructor for the type:
cf = FunctionCompile[{tyDec, funcDec},
Function[Typed[f1, "MachineInteger"],
With[{prod = CreateTypeInstance["myProd", f1]},
prod["field1"]]]
]cf[55]An unmanaged object such as a "CString" can be created, and the result will need to be manually freed:
cf = FunctionCompile[
Function[Typed[len, "MachineInteger"],
Module[{str},
str = CreateTypeInstance["CString", len];
DeleteObject[str]
]]]cf[12]Tech Notes
Related Guides
History
Text
Wolfram Research (2022), CreateTypeInstance, Wolfram Language function, https://reference.wolfram.com/language/ref/CreateTypeInstance.html.
CMS
Wolfram Language. 2022. "CreateTypeInstance." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/CreateTypeInstance.html.
APA
Wolfram Language. (2022). CreateTypeInstance. Wolfram Language & System Documentation Center. Retrieved from 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: 12-June-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: 12-June-2026]}