Time to Meet Your MATCH... (...dialect)

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!

Datatypes match instances of the type

>> match [integer! tag!] 1020
== 1020

>> match [integer! tag!] "this text value won't match"
== ~null~  ; anti

>> match [integer! tag!] <matches!>
== <matches!>

Quoted items match the quoted thing

>> match ['on 'off] second [echo on]
== on

>> match ['on 'off] first [potato]
== ~null~  ; anti

This works for quasiforms:

>> match ['~alpha~ '~[beta gamma]~] second [a ~alpha~ #a]
== ~alpha~

Multiply-quoted items match the quote level minus one:

>> match [''a] first [''a]
== ~null~  ; anti

>> match [''a] first ['a]
== 'a

Single arity actions that return logic can be used

 >> match odd?/ 304
 == ~null~  ; anti

 >> match lib.even?/ 1020
 == 1020

If they're inside a block, you don't need to use a terminal slash to extract them.

 >> match [even? block!] 1020
 == 1020

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~  ; anti

>> typecheck [integer! tag!] [a b c]
== ~null~  ; anti

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~  ; anti

The negated analogue of ENSURE is PROHIBIT

>> prohibit tag! "abc"
== "abc"

>> prohibit tag! <abc>
** Error: PROHIBIT of TAG! received <abc>
1 Like

Very cool. Should be muy bueno for simple lexing and tokenizing values of a dialect-- as well as the handling of syntax errors.

3 posts were split to a new topic: Quirky Ideas From Original MATCH Dialect