Mathematica: How mimic pattern matching of arguments for Boolean?

Icon class
icon_class
far fa-sticky-note
icon_class_computed
far fa-sticky-note
Note kind
Policy level
Webel IT Australia promotes the amazing Mathematica tool and the powerful Wolfram Language and offers professional Mathematica services for computational computing and data analysis. Our Mathematica tips, issue tracking, and wishlist is offered here most constructively to help improve the tool and language and support the Mathematica user community.
DISCLAIMER: Wolfram Research does not officially endorse analysis by Webel IT Australia.
Keywords
Surprisingly there is no "Boolean" in the Wolfram Language, and True and False are just Symbols:

i: Head[True]
o: Symbol

i: Head[False]
o: Symbol

i: testBool = True
i: Head[testBool]
o: Symbol
So pattern matching in function arguments does not behave as one might expect:

i: fNoMatchToBool[arg_Boolean:True] := "Will not actually match True or False";

i: fNoMatchToBool[testBool] (* Not seen, does not match, will just bounce *)
o: fNoMatchToBool[True]
Also, because of the defined argument default, this does work:

I: fNoMatchToBool[]
o: Will not actually match True or False
But you can HACK a match by using an argument value set test:

i: fHackMatchBool[arg:True|False] := "Will match True or False"';
i: fFakeMatchBool[testBool] 
o: "Will match True or False"

Note, however, that if use this trick you can't then specify a Boolean default for the argument.

The most MMA-keen of MMA fans might claim that's not a hack, but certainly the lack of "Boolean" argument matching is a "gotcha". Note that BooleanQ gives True on either True or False, making it slightly more surprising that there isn't comparable argument pattern matching.


I: BooleanQ[testBool]
o: True
Relates to
Related notes
Related notes (backlinks)
Related snippets (extracts)
Visit also
Visit also (backlinks)
External links