ConfirmMatch

ConfirmMatch[expr,form]

confirms that expr matches the pattern form, otherwise throwing an error to the nearest surrounding Enclose.

ConfirmMatch[expr,form,info]

evaluates info and includes its value in the thrown error if expr is not confirmed.

ConfirmMatch[expr,form,info,tag]

uses the specified tag for any thrown errors.

Details

  • ConfirmMatch[expr,] returns expr if expr is successfully confirmed.
  • ConfirmMatch is dynamically scoped when given a tag. Otherwise, it is lexically scoped. »
  • The default value of info is Null.
  • Failure objects generated by ConfirmMatch include the following fields:
  • "Information"info from ConfirmMatch[expr,form,info,]
    "Expression"expr from ConfirmMatch[expr,]
    "Pattern"form from ConfirmMatch[expr,form,]
  • Values of these fields can be extracted using failure["prop"] or directly through Enclose[expr,"prop"].

Examples

open allclose all

Basic Examples  (2)

ConfirmMatch[expr,form] gives expr if expr matches form:

Since "foo" does not match _Integer, ConfirmMatch stops evaluation and throws an error to the surrounding Enclose:

Create a function that checks its arguments:

Scope  (3)

ConfirmMatch stops evaluation and returns to the matching Enclose when it throws an error:

info is only evaluated when expr cannot be confirmed:

Create a function that calls ConfirmMatch with a tag so that it can be caught dynamically:

If no tag is given, ConfirmMatch is lexically scoped:

Applications  (1)

Print a message when a failure is detected:

Properties & Relations  (1)

ConfirmMatch can be emulated by ConfirmBy and MatchQ:

Possible Issues  (3)

ConfirmMatch must be given a tag or be within a lexical Enclose:

Untagged ConfirmMatch works by replacing instances of the symbol ConfirmMatch with a tagged variant:

Tagged ConfirmMatch requires an Enclose with a matching tag:

Wolfram Research (2020), ConfirmMatch, Wolfram Language function, https://reference.wolfram.com/language/ref/ConfirmMatch.html.

Text

Wolfram Research (2020), ConfirmMatch, Wolfram Language function, https://reference.wolfram.com/language/ref/ConfirmMatch.html.

CMS

Wolfram Language. 2020. "ConfirmMatch." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ConfirmMatch.html.

APA

Wolfram Language. (2020). ConfirmMatch. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ConfirmMatch.html

BibTeX

@misc{reference.wolfram_2024_confirmmatch, author="Wolfram Research", title="{ConfirmMatch}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/ConfirmMatch.html}", note=[Accessed: 17-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_confirmmatch, organization={Wolfram Research}, title={ConfirmMatch}, year={2020}, url={https://reference.wolfram.com/language/ref/ConfirmMatch.html}, note=[Accessed: 17-November-2024 ]}