ConfirmAssert[test]
ConfirmAssert[test,info]
若 test 非 True,则对 info 进行计算,并将其值包括在抛出的错误中.
ConfirmAssert[test,info,tag]
为任何抛出的错误使用指定标签.
ConfirmAssert
ConfirmAssert[test]
ConfirmAssert[test,info]
若 test 非 True,则对 info 进行计算,并将其值包括在抛出的错误中.
ConfirmAssert[test,info,tag]
为任何抛出的错误使用指定标签.
更多信息
- ConfirmAssert[…] 总是返回 Null.
- 在给定标签的情况下,ConfirmAssert 的作用域动态变化. 否则,作用域根据语义变化. »
- info 的默认值是 Null.
- ConfirmAssert 生成的 Failure 对象包括下列域:
-
"Information" ConfirmAssert[test,info,…] 的 info "Test" ConfirmAssert[test,…] 的 expr - 可使用 failure["prop"] 或直接通过 Enclose[expr,"prop"] 提取这些域的值.
范例
打开所有单元 关闭所有单元基本范例 (2)
Enclose[ConfirmAssert[2 > 1];2]当断言失败时,停止计算并向周围 Enclose 抛出异常:
Enclose[ConfirmAssert[2 > 3];2]f[x_] := Enclose[ConfirmAssert[Sqrt[x] > 2];Sin[x]]f[5.3]f[3.4]范围 (3)
当 ConfirmAssert 抛出异常时会停止计算并返回其匹配的 Enclose:
Enclose[Echo[1];ConfirmAssert[1 == 0];Echo[2]]Enclose[ConfirmAssert[1 < 5, Echo["info was evaluated"]]]Enclose[ConfirmAssert[12 < 5, Echo["info was evaluated"]]]创建一个可调用带标签的 ConfirmAssert 的函数,这样可以动态捕捉该函数:
f[x_] := g[ConfirmAssert[StringQ[x], Null, "fooTag"];StringReverse[x]]h[y_] := Enclose[f[y], Identity, "fooTag"]h["reverseme"]h[42]若未给定标签,则 ConfirmAssert 的作用域根据语义变化:
f2[x_] := g[ConfirmAssert[StringQ[x]];StringReverse[x]]h2[y_] := Enclose[f2[y]]h2[2]应用 (1)
reverseRoman::nan = "`` is not an integer.";reverseRoman[n_] :=
Enclose[
ConfirmAssert[IntegerQ[n], Message[reverseRoman::nan, n]];
StringReverse[RomanNumeral[n]],
$Failed&]reverseRoman[21]reverseRoman["veni, vidi, vici"]属性和关系 (2)
ConfirmAssert 可用 ConfirmBy 或 ConfirmMatch 进行模仿:
Enclose[ConfirmAssert[False];"hello"]Enclose[ConfirmBy[False, TrueQ];"hello"]Enclose[ConfirmMatch[False, True];"hello"]ConfirmAssert[f[expr]] 有效等价于 ConfirmBy[expr,f],除了一点, ConfirmAssert[f[expr]] 返回 Null 而非 expr:
Enclose[f[ConfirmAssert[StringQ["hello"]]]]Enclose[f[ConfirmBy["hello", StringQ]]]可能存在的问题 (3)
必须给定 ConfirmAssert 一个标签,或在一个语义 Enclose 内:
ConfirmAssert[2 < 4]无标签的 ConfirmAssert 通过用有标签变量替换符号 ConfirmAssert 的实例实现工作:
Enclose[Hold[ConfirmAssert]]有标签的 ConfirmAssert 需要一个有匹配标签的 Enclose:
Enclose[ConfirmAssert[1 < 0, Null, "foo"], Identity, "bar"]文本
Wolfram Research (2020),ConfirmAssert,Wolfram 语言函数,https://reference.wolfram.com/language/ref/ConfirmAssert.html.
CMS
Wolfram 语言. 2020. "ConfirmAssert." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/ConfirmAssert.html.
APA
Wolfram 语言. (2020). ConfirmAssert. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/ConfirmAssert.html 年
BibTeX
@misc{reference.wolfram_2026_confirmassert, author="Wolfram Research", title="{ConfirmAssert}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/ConfirmAssert.html}", note=[Accessed: 14-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_confirmassert, organization={Wolfram Research}, title={ConfirmAssert}, year={2020}, url={https://reference.wolfram.com/language/ref/ConfirmAssert.html}, note=[Accessed: 14-September-2026]}