REBOL 3.0

ASSERT function

Carl Sassenrath, CTO
REBOL Technologies
22-Mar-2009 0:15 GMT

Article #0178
Main page || Index || Prior Article [0177] || Next Article [0179] || 11 Comments || Send feedback

Do you find you often need to check for valid variable values? Me too.

For example, here is part of the make-module intrinsic that checks important fields of a module's specification:

; Validate the important fields:
unless all [
    object? obj: :spec
    block?  obj: :body
    any [
        none? obj: spec/name
        word? spec/name
    ]
    any [
        none? obj: spec/version
        tuple? spec/version
    ]
][
    print ["error in module at:" obj] ; temp
    cause-error 'script 'invalid-spec :obj ; something is wrong here!
]

It's just a bit wordy, isn't it?

That code has just been replaced with the assert native and now looks like this:

assert/type [
    spec object!
    body block!
    spec/name [word! none!]
    spec/version [tuple! none!]
]

It's a bit like a function spec, isn't it? Of course, it also provides the simplified datatype form, as seen in the first two words.

Here's more information about the assert function, which will be released in R3 A38.

Note that assert is very efficient because it does not need to reduce. In addition, it does a simple get on variables; therefore, avoiding problems with possible function evaluation (i.e. it is secure). That means you can use it at the entry point to any section of code that might go wrong if functions are passed.

This is another one of those I've been putting it off for years, but it's used often and worth adding to the language.

11 Comments

REBOL 3.0
Updated 25-Dec-2024 - Edit - Copyright REBOL Technologies - REBOL.net