BQN takes the position that errors exist to indicate exceptional conditions that the developer of a given program didn't expect. However, the types of errors that BQN naturally checks for, such as mismatched shapes in Couple (≍), aren't always enough to detect exceptional conditions. Issues like numeric values that don't make physical sense will slip right through. BQN makes it easy for a programmer to check for these sorts of problems by building in the primitive Assert, written !. This function checks whether 𝕩 matches 1: if it does, then it does nothing and returns 𝕩, and otherwise it gives an error.
! 2=2 # Passed 1 ! 2=3 # Failed Error: Assertion error
To pass, the right argument must be exactly the number 1; any other value causes an error. For example, an array of 1s still causes an error; use ∧´⥊ to convert a boolean array to a single boolean that indicates whether all of its values are true.
! (∧=∨⌾¬)⌜˜ ↕2 Error: 2‿2⥊1‿1‿1‿1 ! ∧´⥊ (∧=∨⌾¬)⌜˜ ↕2 1
Assert can take a left argument, which gives a message to be associated with the error. It's typical to use a string for the left argument in order to display it to the programmer, but the left argument can be any value.
↗️"Message" ! 0 Error: Message ⟨∘,"abc",˜⟩ ! '0' Error: ⟨∘,"abc",˜⟩
Because the left argument to a function is always computed before the function is called, Assert doesn't let you compute the error message only if there's an error. This might be a problem if the error message computation is slow or has side effects. There are a few ways to work around the issue:
Message to compute the message, and call 𝕨 Message⊸!⍟(1⊸≢) 𝕩 or similar instead of !.Message as above, message ! 𝕩 works, and {…}˙⊸! 𝕩 is a convenient syntax for block functions.