Typed[expr,type]
represents an expression that should be assumed to be of a specified type for compilation and other purposes.
Details
- Function[{Typed[x1,type1], …}, …] can be used to specify argument types to be accepted by a Function object for compilation and other purposes.
- The types supported by Typed include:
-
"name" an object with a native type of the specified name {type1,type2,…}type a function taking arguments of type typei and returning the specified type cons::[type1,type2,...] an object with a compound type created using the constructor cons - Native types supported by Typed include:
-
"Boolean" Boolean "ComplexReal64" complex number with IEEE double‐precision real and imaginary parts "Integer8" 8‐bit signed integer "Integer16" 16‐bit signed integer "Integer32" 32‐bit signed integer "Integer64" 64‐bit signed integer "Integer128" 128‐bit signed integer "MachineInteger" machine‐sized signed integer "Real32" IEEE single‐precision real number "Real64" IEEE double‐precision real number "UnsignedInteger8" 8‐bit unsigned integer "UnsignedInteger16" 16‐bit unsigned integer "UnsignedInteger32" 32‐bit unsigned integer "UnsignedInteger64" 64‐bit unsigned integer "UnsignedInteger128" 128‐bit unsigned integer "UnsignedMachineInteger" machine-sized unsigned integer
Examples
open allclose allBasic Examples (4)
Typed specifies the type of function arguments:
FunctionCompile uses Typed annotations for compiling code:
The CompiledCodeFunction evaluates with an argument that matches the type:
If the type of an argument does not match the declared type, an error results:
For computation in the Wolfram Engine, the use of Typed is typically ignored:
is a compound type that takes another type as an argument:The computation proceeds as expected:
A function type can be used an argument to a compiled function:
The computation proceeds as expected:
Typed is also used in FunctionDeclaration:
Possible Issues (1)
Use in Evaluated Code (1)
Typed can be used in the body of a function:
When compiled, it is used to guide the type inferencing process:
However, when evaluated, Typed is treated as an inert expression:
It is better to use TypeHint in the body of a function:
When compiled, it is used to guide the type inferencing process:
However, when evaluated, TypeHint is stripped out:
Text
Wolfram Research (2019), Typed, Wolfram Language function, https://reference.wolfram.com/language/ref/Typed.html.
CMS
Wolfram Language. 2019. "Typed." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Typed.html.
APA
Wolfram Language. (2019). Typed. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Typed.html