LiteralType[x]
represents a literal value x for use as a type.
LiteralType
LiteralType[x]
represents a literal value x for use as a type.
Details
- LiteralType is often used as a parameter of other types. For example, the rank of a packed array is represented with a literal type "PackedArray"::["MachineInteger",LiteralType[2]].
- In some cases, LiteralType is added implicitly, such as in "PackedArray"::["MachineInteger",2].
- Literal types can be integers, strings or Boolean values.
- The head LiteralType can generally be omitted for integer and Boolean values, but is required for string values.
Examples
open all close allBasic Examples (1)
Compile a program for manipulating packed arrays whose types are represented with LiteralType:
cf = FunctionCompile[Function[
Typed[arr, "PackedArray"::["MachineInteger", LiteralType[2]]],
arr ^ 2
]]cf[{{1, 2, 3}, {4, 5, 6}}]Scope (2)
Define a function to have different return types depending on a literal type argument:
decs = {
FunctionDeclaration[operate, Typed[{LiteralType["Square"], "MachineInteger"} -> "MachineInteger"]@
Function[{l, arg}, arg ^ 2]
],
FunctionDeclaration[operate, Typed[{LiteralType["Root"], "MachineInteger"} -> "Real64"]@
Function[{l, arg}, Sqrt[Cast[arg, "Real64"]]]
]
};Compile a function using the first usage:
cf1 = FunctionCompile[decs,
Function[Typed[arg, "MachineInteger"],
operate[LiteralType["Square"], arg]
]]cf1[12]Compile a function using the second usage:
cf1 = FunctionCompile[decs,
Function[Typed[arg, "MachineInteger"],
operate[LiteralType["Root"], arg]
]]cf1[12]Use TypeEvaluate to transform literal types in a function signature:
type = ForAllType[r, {"PackedArray"::["MachineInteger", r]} -> "PackedArray"::["MachineInteger", TypeEvaluate[r + 1]]];Compile a function using the type:
cf = FunctionCompile[{
FunctionDeclaration[addRank, Typed[type]@Function[array, {array}]]
}, Function[Typed[arg, "PackedArray"["MachineInteger", 2]], addRank[arg]]]cf[{{1, 2, 3}, {4, 5, 6}}]Tech Notes
Related Guides
History
Text
Wolfram Research (2022), LiteralType, Wolfram Language function, https://reference.wolfram.com/language/ref/LiteralType.html.
CMS
Wolfram Language. 2022. "LiteralType." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/LiteralType.html.
APA
Wolfram Language. (2022). LiteralType. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LiteralType.html
BibTeX
@misc{reference.wolfram_2026_literaltype, author="Wolfram Research", title="{LiteralType}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/LiteralType.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_literaltype, organization={Wolfram Research}, title={LiteralType}, year={2022}, url={https://reference.wolfram.com/language/ref/LiteralType.html}, note=[Accessed: 12-June-2026]}