WOLFRAM

TypeOf[expr]

gives the type of expr without evaluating it.

TypeOf[expr,Typed[x,ty]]

assumes that instances of x in expr have type ty.

TypeOf[expr,{Typed[x1,ty1],Typed[x2,ty2],}]

assumes that xi has type tyi.

TypeOf[expr,decls]

uses declarations decls.

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 allclose all

Basic Examples  (6)Summary of the most common use cases

Find the type of a function::

Out[1]=1

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:

Out[1]=1

Find the type of an expression while specifying only the type of a subexpression:

Out[1]=1

Find the type of a function using a FunctionDeclaration:

Out[2]=2

Compile a program that contains TypeOf:

Out[1]=1
Out[2]=2

TypeOf is useful in polymorphic declarations written with ForAllType because the types of arguments are not known when the declaration is made:

Out[1]=1
Out[2]=2

TypeOf can be used with TypeEvaluate to determine the type in a declaration:

Out[2]=2
Out[3]=3

Scope  (2)Survey of the scope of standard use cases

Find the type of a program using arrays:

Out[1]=1

TypeOf does not evaluate its argument in compiled code, just as in evaluated code:

Out[1]=1
Out[2]=2

Applications  (1)Sample problems that can be solved with this function

Find the results of multiplying different types together:

Out[3]=3

Properties & Relations  (3)Properties of the function, and connections to other functions

TypeOf infers the type of its input without evaluating it:

Out[1]=1

TypeOf gives the return type of the CompiledCodeFunction generated by FunctionCompile but is much faster:

Out[1]=1
Out[2]=2

If TypeOf is unable to compile something, it issues a message that typically contains a button to show the source of the error:

Out[1]=1

Clicking the source button typically opens a user interface that can help to locate the error:

Possible Issues  (3)Common pitfalls and unexpected behavior

TypeOf can only infer the types of compilable programs:

Out[1]=1

TypeOf does not evaluate its argument, which leads to unintended consequences:

Out[1]=1

The argument of TypeOf may need explicit evaluation:

Out[2]=2

TypeOf only computes the type of its primary argument, so does not find errors in declarations:

Out[1]=1

FunctionCompile does a full compilation and can find errors in declarations:

Out[2]=2
Wolfram Research (2022), TypeOf, Wolfram Language function, https://reference.wolfram.com/language/ref/TypeOf.html.
Wolfram Research (2022), TypeOf, Wolfram Language function, https://reference.wolfram.com/language/ref/TypeOf.html.

Text

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

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.

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

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

BibTeX

@misc{reference.wolfram_2025_typeof, author="Wolfram Research", title="{TypeOf}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TypeOf.html}", note=[Accessed: 02-June-2025 ]}

@misc{reference.wolfram_2025_typeof, author="Wolfram Research", title="{TypeOf}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TypeOf.html}", note=[Accessed: 02-June-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_typeof, organization={Wolfram Research}, title={TypeOf}, year={2022}, url={https://reference.wolfram.com/language/ref/TypeOf.html}, note=[Accessed: 02-June-2025 ]}

@online{reference.wolfram_2025_typeof, organization={Wolfram Research}, title={TypeOf}, year={2022}, url={https://reference.wolfram.com/language/ref/TypeOf.html}, note=[Accessed: 02-June-2025 ]}