37 lines
1.4 KiB
Common Lisp
37 lines
1.4 KiB
Common Lisp
;;; CONDITIONS 6: RESTARTS WITH ARGUMENTS, IGNORE-ERRORS
|
|
;;;
|
|
;;; Exercise 4's restart carried no luggage. USE-VALUE takes an
|
|
;;; argument - a replacement the handling code chooses - and
|
|
;;; STORE-VALUE is its cousin for storing one and trying again.
|
|
;;; INVOKE-RESTART passes it over:
|
|
;;;
|
|
;;; (handler-bind ((parse-error
|
|
;;; (lambda (c) (invoke-restart 'use-value 42))))
|
|
;;; (parse-count "abc"))
|
|
;;;
|
|
;;; IGNORE-ERRORS wraps a form in cotton wool: anything it signals
|
|
;;; turns into NIL, and the condition rides along as a second
|
|
;;; value.
|
|
|
|
;; TODO: Parse TEXT as an integer - but offer a USE-VALUE restart
|
|
;; that answers with whatever value the caller hands over.
|
|
(defun parse-count (text)
|
|
(parse-integer text))
|
|
|
|
;; TODO: The integer in TEXT, or NIL when TEXT does not parse.
|
|
(defun maybe-age (text)
|
|
(parse-integer text))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check-equal 42 (handler-bind ((parse-error (lambda (c)
|
|
(declare (ignore c))
|
|
(invoke-restart 'use-value 42))))
|
|
(parse-count "abc")))
|
|
(check-equal 7 (parse-count "7"))
|
|
(check-equal 42 (maybe-age "42"))
|
|
(check-equal nil (maybe-age "abc"))
|