TypeOf
Details and Options
- TypeOf can be used in both interpreted and compiled programs.
- TypeOf uses the type inferencing system of the Wolfram Compiler.
- TypeOf only accepts expressions and types that can be compiled by the Wolfram Compiler.
- TypeOf can be used in interpreted code as a quick check whether compilation will work.
- TypeOf can be used in compiled code to determine the type of some part of the code.
- TypeOf can be used with TypeEvaluate to direct the type for a function declaration.
- TypeOf can take both Typed and declaration specifications such as FunctionDeclaration and TypeDeclaration.
Examples
open all close allBasic Examples (6)
TypeOf[Function[{Typed[x, "Real64"], Typed[y, "Integer64"]}, x + y]]TypeOf is useful since it just determines the type of a function, giving an indication that compilation may work.
Find the type of an expression:
TypeOf[5040]Find the type of an expression while specifying only the type of a subexpression:
TypeOf[StringLength[str] ^ 2 + 3.6, Typed[str, "String"]]Find the type of a function using a FunctionDeclaration:
decl = FunctionDeclaration[SubFun, Typed[{"Integer64"} -> "Integer64"]@Function[arg, arg + 1]];
TypeOf[Function[Typed[arg, "Integer64"], SubFun[arg]], decl]Compile a program that contains TypeOf:
cf = FunctionCompile[Function[Typed[arg, "Real64"], Cast[5040, TypeOf[arg]]]]cf[4.5]TypeOf is useful in polymorphic declarations written with ForAllType because the types of arguments are not known when the declaration is made:
decl = FunctionDeclaration[MyIncrement, Typed[ForAllType[e, {e} -> e]]@Function[{arg}, arg + TypeHint[1, TypeOf[arg]]]];
cf = FunctionCompile[decl, Function[Typed[arg, "Integer8"], MyIncrement[arg]]]cf[4]TypeOf can be used with TypeEvaluate to determine the type in a declaration:
type = ForAllType[{elem},
Element[elem, "Numbers"],
{elem} -> TypeEvaluate[TypeOf[Re[elem]]]];cf = FunctionCompile[FunctionDeclaration[MyFun1, Typed[type]@Function[x, Re[x]]], Function[Typed[arg, "ComplexReal64"], MyFun1[arg]]]cf[4. + 5. I]Scope (2)
Find the type of a program using arrays:
TypeOf[Table[a + b, {a, 10}, {b, 5}].Range[5]]TypeOf does not evaluate its argument in compiled code, just as in evaluated code:
cf = FunctionCompile[Function[{}, TypeOf[Echo["hello"];2 + 3]]]cf[]Applications (1)
Properties & Relations (3)
TypeOf infers the type of its input without evaluating it:
TypeOf[Echo[123] + 2.5]TypeOf gives the return type of the CompiledCodeFunction generated by FunctionCompile but is much faster:
AbsoluteTiming@TypeOf[StringLength["hello"] ^ 2 + 3.6]AbsoluteTiming@FunctionCompile[Function[{}, StringLength["hello"] ^ 2 + 3.6]]If TypeOf is unable to compile something, it issues a message that typically contains a button to show the source of the error:
TypeOf[Function[Typed[arg, "Real64"], Module[{size = arg < 10}, Sin[size]]]]Clicking the source button typically opens a user interface that can help to locate the error:
[image]Possible Issues (3)
TypeOf can only infer the types of compilable programs:
TypeOf[uncompilableFunc[2]]TypeOf does not evaluate its argument, which leads to unintended consequences:
f = Function[Typed[arg, "Integer64"], arg];
TypeOf[f]The argument of TypeOf may need explicit evaluation:
f = Function[Typed[arg, "Integer64"], arg];
TypeOf[Evaluate[f]]TypeOf only computes the type of its primary argument, so does not find errors in declarations:
decl = FunctionDeclaration[SubFun, Typed[{"Integer64"} -> "Integer64"]@Function[arg, arg1]];
TypeOf[Function[Typed[arg, "Integer64"], SubFun[arg]], decl]FunctionCompile does a full compilation and can find errors in declarations:
decl = FunctionDeclaration[SubFun, Typed[{"Integer64"} -> "Integer64"]@Function[arg, arg1]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], SubFun[arg]]]Tech Notes
Related Guides
History
Text
Wolfram Research (2022), TypeOf, Wolfram Language function, https://reference.wolfram.com/language/ref/TypeOf.html.
CMS
Wolfram Language. 2022. "TypeOf." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/TypeOf.html.
APA
Wolfram Language. (2022). TypeOf. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TypeOf.html
BibTeX
@misc{reference.wolfram_2026_typeof, author="Wolfram Research", title="{TypeOf}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TypeOf.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_typeof, organization={Wolfram Research}, title={TypeOf}, year={2022}, url={https://reference.wolfram.com/language/ref/TypeOf.html}, note=[Accessed: 13-June-2026]}