Alberto Riva
1/6/2016 10:52:00 PM
On 01/06/2016 05:44 PM, Alberto Riva wrote:
> On 01/06/2016 04:08 AM, Jim Newton wrote:
>> However, sometimes, I eliminate cases altogether (for example if the
>> first case it the t type. E.g.
>> (reduced-typecase obj
>> ((or A (not A)) (F))
>> (t (G)))
>> -->
>> (progn obj (F))
>>
>> In such a case I'd like the compiler to warn that dead code was
>> removed, provided that code was written by me.
>>
>> What is the correct way to handle this?
>
> In this case, if I understand correctly, *your* macro is doing the
> removal, so your macro should print a warning when it does that. If your
> macro removes the code, the compiler never sees it, so it has no way of
> knowing that it was removed.
For example, let's say you want to write a macro that wraps its
arguments in a PROGN, eliminating those that are NIL (it's a silly
example because they would not have any effect anyway, except in the
last position, but it's just to show what I mean).
You can write:
(defmacro cleaned-progn (&rest forms)
(let* ((removed 0)
(valid (loop for f in forms
if f collect f
else do (incf removed))))
(warn "~a empty forms removed." removed)
`(progn ,@valid)))
CLEANED-PROGN
> (macroexpand '(cleaned-progn (do-a) nil (do-b) nil (do-c)))
Warning: 2 empty forms removed.
(PROGN (DO-A) (DO-B) (DO-C))
T
Alberto