>>>>> "mr" == Matthias Rinck <[EMAIL PROTECTED]> writes:

  mr> is it possible to redefine a structure without getting an error-message?
  mr> 
  mr> 1) (defstruct x a b)
  mr> 2) (defstruct x a b c) -> error ... [CLOBBER-IT] Smash current layout,
  mr> preserving old code
  mr> 
  mr> Is it possible to avoid the error and directly ``CLABBER-IT''

yes, support for this sort of thing is integrated in the Common Lisp
condition system. If you attempt to redefine a structure
interactively, CMUCL will put you in the debugger and give you a
choice of restarts:

,----
| Restarts:
|   0: [continue  ] Invalidate already loaded code and instances, use new definition.
|   1: [clobber-it] Assume redefinition is compatible, allow old code and instances.
|   2: [abort     ] Return to Top-Level.
`----

The ABORT restart is supplied by the debugger, but the CONTINUE and
CLOBBER-IT restarts are supplied by the CMUCL function
KERNEL::%REDEFINE-DEFSTRUCT that handles structure redefinition. As
well as selecting restarts interactively, as you do from the debugger,
the Common Lisp condition system allows you to handle them
computationally, via the functions FIND-RESTART and INVOKE-RESTART.
You need to set up a handler for the condition that is signalled (in
this case it is a simple ERROR), then find the restart that you're
looking for, then invoke it. For example:
  
,----
| (defstruct foo one two)
| 
| (handler-bind ((error (lambda (c)
|                         (let ((r (find-restart 'kernel::clobber-it c)))
|                           (when r (invoke-restart r))))))
|   (defstruct foo one two three))
`----
  
This would be a little prettier if CMUCL raised a more specific
condition than the generic ERROR in this case, and if the symbol
CLOBBER-IT were exported from the KERNEL package, but redefining
structures indiscriminately in this way is likely to lead to problems,
so I don't think it would be good for CMUCL to encourage this
particular use of restarts.

For more information on the Common Lisp condition system, see

   <URL:http://www.psg.com/~dlamkins/sl/chapter23.html>
   <URL:http://world.std.com/~pitman/Papers/Condition-Handling-2001.html>

and of course the HyperSpec.

-- 
Eric Marsden                          <URL:http://www.laas.fr/~emarsden/>

Reply via email to