Mathematica: How mimic pattern matching of arguments for pseudo "Boolean" (including providing a default)?

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.
Icon class
icon_class
far fa-sticky-note
icon_class_computed
far fa-sticky-note
Note kind
Policy level
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 new-comers might expect:

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

(* test: testBool = True *)
i: fNoMatchToBool[testBool] (* Not seen, pattern 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, and also still provide a default (which is specified as False here):

i: fHackMatchBool[arg:(True|False):False] := ToString[arg] <> ": Will match True or False."';

(* test *)
i: fFakeMatchBool[True] 
o: "True: Will match True or False."
i: fFakeMatchBool[False] 
o: "False: Will match True or False".
i: fFakeMatchBool[] 
o: "False: Will match True or False".

(The most devoted of Mathematica fans might claim that's not a hack, but certainly the lack of "Boolean" argument matching may be a confusing "gotcha" for new-comers.)

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