assert / check-type / assure...

Gail Zacharias gz at clozure.com
Sun Sep 22 23:06:50 UTC 2013


ccl has a function (not a macro) called require-type, as in
 (1+ (require-type x 'number)).  Very useful indeed.



On Sun, Sep 22, 2013 at 9:53 AM, Pascal Costanza <pc at p-cos.net> wrote:

> Hi,
>
> It seems to me that ASSERT and CHECK-TYPE are not as convenient as they
> could be. In particular, ISLISP seems to have a better alternative in
> ASSURE.
>
> ASSURE is easy to define:
>
> (defmacro assure (type form)
>   (let ((object (copy-symbol 'object)))
>     `(let ((,object ,form))
>        (check-type ,object ,type)
>        ,object)))
>
> The important difference is that the value of form is returned, which
> allows using ASSURE inline in expressions:
>
> (1+ (assure number x))
>
> …in place of the more lengthy:
>
> (progn
>   (check-type x number)
>   (1+ x))
>
> Is ASSURE, or something similar, part of any utility library, like
> Alexandria or the likes?
>
> On an unrelated note, I recently came up with the following utility macro
> which I found very useful:
>
> (defmacro assocf (item alist &optional default &rest keys &key test
> test-not key)
>   (declare (ignore test test-not key))
>   (let ((it (copy-symbol 'it)) (cons (copy-symbol 'cons)))
>     `(let* ((,it ,item) (,cons (assoc ,it ,alist , at keys)))
>        (unless ,cons
>          (setf ,cons (cons ,it ,default)
>                ,alist (cons ,cons ,alist)))
>        ,cons)))
>
> Again, is something like this already part of some utility library?
>
>
> Thanks,
> Pascal
>
> --
> Pascal Costanza
> The views expressed in this email are my own, and not those of my employer.
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/pro/attachments/20130922/e5bdf97d/attachment.html>


More information about the pro mailing list