Typed[expr,type]
表示一个表达式,在编译或用于其他目的时,应假定它的类型为指定类型.
Typed
编译类型列表 »Typed[expr,type]
表示一个表达式,在编译或用于其他目的时,应假定它的类型为指定类型.
更多信息
- Function[{Typed[x1,type1], …}, …] 可用来指定在编译或用于其他目的时 Function 对象接受的参数类型.
- Typed 支持的类型包括:
-
"name" 具有指定名称的本机类型的对象 {type1,type2,…}type 接受类型为 typei 的参数的函数,并返回指定的类型 cons::[type1,type2,...] 用构造 cons 创建的复合类型的对象 - Typed 支持的本机类型包括:
-
"Boolean" 布尔 "ComplexReal64" 带有 IEEE 双精度实部和虚部的复数 "Integer8" 8‐bit 有符号整数 "Integer16" 16‐bit 有符号整数 "Integer32" 32‐bit 有符号整数 "Integer64" 64‐bit 有符号整数 "Integer128" 128‐bit 有符号整数 "MachineInteger" 机器大小的符号整数 "Real32" IEEE 单精度实数 "Real64" IEEE 双精度实数 "UnsignedInteger8" 8‐bit 无符号整数 "UnsignedInteger16" 16‐bit 无符号整数 "UnsignedInteger32" 32‐bit 无符号整数 "UnsignedInteger64" 64‐bit 无符号整数 "UnsignedInteger128" 128‐bit 无符号整数 "UnsignedMachineInteger" 机器大小的无符号整数
范例
打开所有单元 关闭所有单元基本范例 (4)
Typed 指定函数参数的类型:
f = Function[Typed[arg, "MachineInteger"], arg + 1]编译代码时,FunctionCompile 使用 Typed 注释:
cf = FunctionCompile[f]CompiledCodeFunction 使用与类型匹配的参数进行计算:
cf[10]cf[10.5]cf = FunctionCompile@Function[Typed[arg, "ListVector"::["String"]], AnyTrue[arg, StringLength[#] > 4&]
]Information[cf, "Type"]cf[{"ab", "cd", "ef", "abcde"}]cf = FunctionCompile@Function[{Typed[funArg, {"MachineInteger"} -> "MachineInteger"], Typed[n, "MachineInteger"]}, funArg[n]];
cfArg = FunctionCompile[Function[Typed[arg, "MachineInteger"], arg + 1]]cf[cfArg, 10]也可在 FunctionDeclaration 中使用 Typed:
decl = FunctionDeclaration[
AddTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2 + arg]
]然后将该声明提供给 FunctionCompile:
cf = FunctionCompile[decl, Function[Typed[arg, "Integer64"], AddTwo[arg]]]cf[10]属性和关系 (2)
当在纯函数的参数列表中使用时,在非编译计算中 Typed 实际上被忽略:
Function[{Typed[x, "Real64"]}, x + 1]["two"]当在纯函数主体中使用时,在普通的 Wolfram 语言计算中保持原样:
Function[{x}, x + Typed[1, "Real64"]]["two"]Typed 在常规计算时是惰性的:
Typed[x, "Boolean"]TypeHint,也可在函数中使用,在正常计算时,结果为其第一个参数:
TypeHint[x, "Boolean"]可能存在的问题 (1)
在已计算过的代码中使用 (1)
可在函数体中使用 Typed:
func = Function[{}, Typed[1, "UnsignedInteger8"]];
comp = FunctionCompile[func]comp[]但是,计算时 Typed 被视为惰性表达式:
func[]在函数体中使用 TypeHint 更合适:
func = Function[{}, TypeHint[1, "UnsignedInteger8"]];
comp = FunctionCompile[func]comp[]但是,计算时 TypeHint 被剥离:
func[]技术笔记
文本
Wolfram Research (2019),Typed,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Typed.html.
CMS
Wolfram 语言. 2019. "Typed." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/Typed.html.
APA
Wolfram 语言. (2019). Typed. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Typed.html 年
BibTeX
@misc{reference.wolfram_2026_typed, author="Wolfram Research", title="{Typed}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/Typed.html}", note=[Accessed: 26-October-2026]}
BibLaTeX
@online{reference.wolfram_2026_typed, organization={Wolfram Research}, title={Typed}, year={2019}, url={https://reference.wolfram.com/language/ref/Typed.html}, note=[Accessed: 26-October-2026]}