assert / check-type / assure...

Pascal Costanza pc at p-cos.net
Sun Sep 22 17:39:07 UTC 2013


This seems to help if you know upfront how many values you are dealing with.

However, the parameter list of assure is (type form). To the best of my knowledge, there is no portable way to expand a type to see if it expands to a values form, which would give you chance to construct a form that knows the number of values.

Without that knowledge, you would have to construct a form that is agnostic in that regard. However:

(defmacro assure (type form)
  (let ((values (copy-symbol 'values)))
    `(let ((,values (multiple-value-list ,form)))
       (check-type (apply 'values ,values) ,type)
       (apply 'values ,values))))

This is not a portable definition, since (apply 'values …) is not a portable place.

typep also doesn't help. Any other options?


Pascal

On 22 Sep 2013, at 19:19, Faré <fahree at gmail.com> wrote:

> See fare-utils:define-values-modify-macro
> 
> (defmacro define-values-modify-macro (name val-vars lambda-list function)
>  "Multiple-values variant on define-modify macro, by Tim Moore"
>  (let ((env (gensym "ENV")))
>    `(defmacro ,name (, at val-vars , at lambda-list &environment ,env)
>      (multiple-value-bind (vars vals store-vars writer-form reader-form)
>          (get-setf-expansion `(values ,, at val-vars) ,env)
>        (let ((val-temps (mapcar #'(lambda (temp) (gensym (symbol-name temp)))
>                                 ',val-vars)))
>          `(let* (,@(mapcar #'list vars vals)
>                  , at store-vars)
>            (multiple-value-bind ,val-temps ,reader-form
>              (multiple-value-setq ,store-vars
>                (,',function , at val-temps ,, at lambda-list)))
>              ,writer-form
>            (values , at store-vars)))))))
> —♯ƒ • François-René ÐVB Rideau •Reflection&Cybernethics• http://fare.tunes.org
> 
> 
> On Sun, Sep 22, 2013 at 1:15 PM, Pascal Costanza <pc at p-cos.net> wrote:
>> 
>> On 22 Sep 2013, at 18:44, Steve Haflich <shaflich at gmail.com> wrote:
>> 
>> As language lawyer, I must point out that the proposed definition of assure
>> is not compatible with places that are multiple-valued.  check-type is
>> required to be (because the ANS mentions no exception about it) although I'm
>> sure lots of implementations get this wrong.
>> 
>> 
>> Another good point. However, this seems to suggest that it's not possible to
>> implement ASSURE portably if the goal is to cover multiple values as well.
>> Or am I missing something?
>> 
>> Pascal
>> 
>> On Sun, Sep 22, 2013 at 6: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.
>>> 
>>> 
>>> 
>>> 
>> 
>> 
>> --
>> Pascal Costanza
>> The views expressed in this email are my own, and not those of my employer.
>> 
>> 
>> 

--
Pascal Costanza






More information about the pro mailing list