MATCH is a handy tool for testing a value against some basic rules, and passing it through if they match...or evaluating to null if they don't.
It uses the "match dialect", which is the same thing that typechecking function parameters uses!
WORD!s match instances of the type constraint
>> match [integer! tag!] 1020
== 1020
>> match [integer! tag!] "this text value won't match"
== \~null~\ ; antiform
>> match [integer! tag!] <matches!>
== <matches!>
This works for functions, too (if they take a single argument and return a LOGIC)
>> match [even?] 1020
== 1020
>> match [odd?] 1020
== \~null~\ ; antiform
Quoted, Quasiform, or Sigil'd WORD! Matches The Decoration
If the decoration matches, it will run the constraint... and if both match, it matches:
>> match ['word!] first ['a]
== 'a
>> match [word!] first ['a]
== \~null~\ ; antiform
>> match [@even?] first [@1020]
== @1020
You can put literal matches in QUASI-BLOCK!s
It works just as in datatype specs:
>> match [~[on off]~] second [echo on]
== on
>> match [~[on off]~] first [potato]
== \~null~\ ; antiform
But as an added bonus, MATCH accepts SPLICE!, so you can use it without the enclosing block!
>> match ~[on off]~ second [echo on]
== on
MATCH has an automatic erroring form, called ENSURE
If you want a quick and dirty way to typecheck something and pass it through, but error otherwise, use ENSURE.
>> ensure [empty?] [a b]
** Script Error: ...
>> ensure [empty?] []
== []
There's also TYPECHECK which returns ~null~ or ~okay~
If you just want a yes or no answer, then you can use TYPECHECK
>> typecheck [integer! tag!] <abc>
== \~okay~\ ; antiform
>> typecheck [integer! tag!] [a b c]
== \~null~\ ; antiform
This is useful if you want to test for things like NULL itself, because MATCH gives an error in these cases:
>> match [<null> integer!] null
** Error: MATCH can't match null (reserved to mean non-match)
>> typecheck [<null> integer!] null
== \~okay~\ ; anti
The Opposite of MATCH is NON
>> non integer! <abc>
== <abc>
>> non integer! 1020
== \~null~\ ; antiform (logic!)
The negated analogue of ENSURE is PROHIBIT
>> prohibit tag! "abc"
== "abc"
>> prohibit tag! <abc>
** Error: PROHIBIT of TAG! received <abc>