Lisp gives you options. Hopefully. One day, I might even be able to use them somewhat.
Here's my grossly simplified, probably wrong internal model of conditions:
- Something goes wrong
- A continuation is created, or maybe several are. Conditions are mystical "pause" buttons in the app, or something.
- The debugger appears, offering you the options
- When you pick one, lisp magicaly re-injects you where the condition was created.
- Things hopefully work a bit better.
Any way. From what I can gather, the CL condition system is implemented in terms of objects, and a 'condition' is a parent of an 'error'. 'Conditions' allow for signalling of diferent states within your app, but that's quite a fancy-pants subject, for when I know what the heck I'm doing. Typically, you'd define your error condition like this:
(define-condition slave-not-happy (error) ... other code ...)
So you're basically creating your little error as a sub-class of error, which is a subclass of condition.
In your code, you can then enable all sorts of mystical getting-out-of-jail stuff like this:
... other code ... (restart-case (error 'slave-not-happy :useable-key-name useable-key-value
(this-option ... some code)
(that-option ... some other code))
...So when you have a problem, and lisp kicks into the debugger, you'll have something like
..blah blah blah - THERE'S A PROBLEM:
0 - this-option
1 - that-option
2 - abort
3 - maybe some more options
Your stuff appears RIGHT THERE with the system's restarts, and you feel all powerful and stuff. So, basically, you're giving yourself more options for getting out of trouble.
Deciding what to do with the signalled conditions is up to you, but you can start getting really fancy-cool stuff done:
...code
(handler-bind ((slave-not-happy #'(lambda (c) (invoke-restart 'this-option)))
... other code
Of course, you can conditionally decide which restart to invoke, based on some other internal logic. Really spiffy.
here's a complete, working example you can put in a file and play around with:
Also available at
http://ryan-white-lisp.googlecode.com/files/conditions-atomic.lisp
Update:
Nikodemus Siivola (major contributor to SBCL) posted a style guide on conditions and errors:
"Learn the difference between HANDLER-BIND and HANDLER-CASE. Short version: HANDLER-CASE always unwinds, with HANDLER-BIND you can eg. log the condition without handling it, or decide whether to unwind or not after inspecting the condition in more detail."
Fantastic resources:
http://www.nhplace.com/kent/Papers/Condition-Handling-2001.html (Kent Pitman)
http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html (Peter Siebel)
enjoy.
#|
Copyright (c) 2008 Ryan White
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
CONDITIONS - quick and dirty intro/reminder
Originally set up on
Fire up lisp and enter
(load "path-to-this-file")
You should then be able to call
(slave 10)
which will return 10
(slave "foo")
which will give you a set of restarts.
Calling
(master '(1 2 3 "foo"))
will return just (1 2 3)
(master '(1 2 3 "foo") :on-error "change" :chage-value 111)
will return (1 2 3 111)
(emperor '(1 2 3 "foo"))
will return (1 2 3)
(careless-master '(1 2 3 "foo"))
will give a set of restarts
(micromanaging-emperor '(1 2 3 "foo"))
will return (1 2 3 1000000)
Extend.Document.Publish.
|#
#|
Define a condition - an indicator that something isn't
totally and fantastically 100% right
|#
(define-condition slave-not-happy (error)
((given-value :initarg :given-value :reader given-value-read))
(:report (lambda (condition stream)
(format stream "~A is not a number, dammit!" (given-value-read condition)))))
#|
This is the function at the bottom of the chain of events.
When its unhappy, it uses the condition to let everyone know
|#
(defun slave (item)
(if (numberp item) ;the test
item ;if everything's good, just return the number
(restart-case (error 'slave-not-happy :given-value item) ;or start this whole error handling trip
(use-nil () :report "Use nil value." nil)
(replace-item (new-value) :report "Replace the value" new-value)
(replace-item-interactively (new-value)
:report "Provide a value yourself."
:interactive (lambda ()
(format t "~&Value to use: ")
(list (eval (read))))
(slave new-value))))) ;finally - a potentially recursive call...
#|
This is a function up the chain of events. It handles the error spit out
|#
(defun master (mylist &key (on-error "ignore") (change-value 0))
(handler-bind ((slave-not-happy ;figuring out what to do with the slave-not-happy error
(if (equal on-error "change")
#'(lambda (c) (invoke-restart 'replace-item change-value)) ;we either do a substitution
#'(lambda (c) (invoke-restart 'use-nil)) ; or we use nil
)))
(let ((output '()) (tst ""))
(loop for item in mylist do
(setf tst (slave item))
(if (eq nil tst)
()
(push tst output)))
(reverse output))))
#|
This function just orders stuff done, and doesn't want to know
about the details
|#
(defun emperor (list)
(master list))
#|
This version of master doesn't worry about errors at all.
Basically, it just doesn't cater for the error
|#
(defun careless-master (mylist &key)
(let ((output '()) (tst nil))
(loop for item in mylist do
(setf tst (slave item))
(if (eq nil tst)
()
(push tst output)))
(reverse output)))
#|
If the emperor wishes, he can do the managing of
the unhappy slave. In a typically self-serving manner
|#
(defun micromanaging-emperor (list)
(handler-bind
((slave-not-happy #'(lambda (c) (invoke-restart 'replace-item 1000000))))
(careless-master list)))
No comments:
Post a Comment