ConfirmBy
Details
- ConfirmBy[expr,…] returns expr if expr is successfully confirmed.
- ConfirmBy is dynamically scoped when given a tag. Otherwise, it is lexically scoped. »
- The default value of info is Null.
- Failure objects generated by ConfirmBy include the following fields:
-
"Information" info from ConfirmBy[expr,f,info,…] "Expression" expr from ConfirmBy[expr,…] "Function" f from ConfirmBy[expr,f,…] - Values of these fields can be extracted using failure["prop"] or directly through Enclose[expr,"prop"].
Examples
open all close allBasic Examples (2)
ConfirmBy[expr,f] gives expr if f[expr] evaluates to True:
Enclose[f[1, ConfirmBy[2, NumberQ], 3]]Since "foo" is not a number, ConfirmBy stops evaluation and throws an error to the surrounding Enclose:
Enclose[f[1, ConfirmBy["foo", NumberQ], 3]]Create a function that checks its arguments:
world[x_] := Enclose[ConfirmBy[x, StringQ] <> ", World!"]world["Hello"]world[4]Scope (3)
ConfirmBy stops evaluation and returns to the matching Enclose when it throws an error:
Enclose[Echo[1];ConfirmBy[10, LessThan[5]];Echo[2]]info is only evaluated when expr cannot be confirmed:
Enclose[ConfirmBy[2, NumberQ, Echo["info was evaluated"]]]Enclose[ConfirmBy["NaN", NumberQ, Echo["info was evaluated"]]]Create a function that calls ConfirmBy with a tag so that it can be caught dynamically:
f[x_] := g[StringReverse[ConfirmBy[x, StringQ, Null, "fooTag"]]]h[y_] := Enclose[f[y], Identity, "fooTag"]h["reverseme"]h[42]If no tag is given, ConfirmBy is lexically scoped:
f2[x_] := g[StringReverse[ConfirmBy[x, StringQ]]]h2[y_] := Enclose[f2[y]]h2[2]
Applications (1)
Print a message when a failure is detected:
reverseRoman::nan = "`` is not an integer.";reverseRoman[n_] :=
Enclose[
StringReverse[RomanNumeral[
ConfirmBy[n, IntegerQ, Message[reverseRoman::nan, n]]
]],
$Failed&]reverseRoman[21]reverseRoman["veni, vidi, vici"]Properties & Relations (1)
ConfirmBy can be emulated with ConfirmMatch:
Enclose[f[ConfirmBy["foo", NumberQ]]]Enclose[f[ConfirmMatch["foo", _ ? NumberQ]]]Possible Issues (3)
ConfirmBy must be given a tag or be within a lexical Enclose:
ConfirmBy[5, NumberQ]Untagged ConfirmBy works by replacing instances of the symbol ConfirmBy with a tagged variant:
Enclose[Hold[ConfirmBy]]Tagged ConfirmBy requires an Enclose with a matching tag:
Enclose[ConfirmBy["hello", NumberQ, Null, "foo"], Identity, "bar"]
See Also
Related Guides
History
Text
Wolfram Research (2020), ConfirmBy, Wolfram Language function, https://reference.wolfram.com/language/ref/ConfirmBy.html.
CMS
Wolfram Language. 2020. "ConfirmBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ConfirmBy.html.
APA
Wolfram Language. (2020). ConfirmBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ConfirmBy.html
BibTeX
@misc{reference.wolfram_2026_confirmby, author="Wolfram Research", title="{ConfirmBy}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/ConfirmBy.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_confirmby, organization={Wolfram Research}, title={ConfirmBy}, year={2020}, url={https://reference.wolfram.com/language/ref/ConfirmBy.html}, note=[Accessed: 12-June-2026]}