TypeOf

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)

Find the type of a function::

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:

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

Find the type of a function using a FunctionDeclaration:

Compile a program that contains TypeOf:

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

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

Scope  (2)

Find the type of a program using arrays:

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

Applications  (1)

Find the results of multiplying different types together:

Properties & Relations  (3)

TypeOf infers the type of its input without evaluating it:

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

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

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

Possible Issues  (3)

TypeOf can only infer the types of compilable programs:

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

The argument of TypeOf may need explicit evaluation:

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

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

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.

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_2024_typeof, author="Wolfram Research", title="{TypeOf}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TypeOf.html}", note=[Accessed: 20-January-2025 ]}

BibLaTeX

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