Commit | Line | Data |
---|---|---|
07d83abe MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
9dfcd9e2 | 3 | @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009, 2010, 2011 |
07d83abe MV |
4 | @c Free Software Foundation, Inc. |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
07d83abe MV |
7 | @node Control Mechanisms |
8 | @section Controlling the Flow of Program Execution | |
9 | ||
10 | See @ref{Control Flow} for a discussion of how the more general control | |
11 | flow of Scheme affects C code. | |
12 | ||
13 | @menu | |
14 | * begin:: Evaluating a sequence of expressions. | |
15 | * if cond case:: Simple conditional evaluation. | |
16 | * and or:: Conditional evaluation of a sequence. | |
17 | * while do:: Iteration mechanisms. | |
17ed90df AW |
18 | * Prompts:: Composable, delimited continuations. |
19 | * Continuations:: Non-composable continuations. | |
07d83abe MV |
20 | * Multiple Values:: Returning and accepting multiple values. |
21 | * Exceptions:: Throwing and catching exceptions. | |
22 | * Error Reporting:: Procedures for signaling errors. | |
661ae7ab | 23 | * Dynamic Wind:: Dealing with non-local entrance/exit. |
07d83abe | 24 | * Handling Errors:: How to handle errors in C code. |
ce2612cd | 25 | * Continuation Barriers:: Protection from non-local control flow. |
07d83abe MV |
26 | @end menu |
27 | ||
28 | @node begin | |
29 | @subsection Evaluating a Sequence of Expressions | |
30 | ||
31 | @cindex begin | |
32 | @cindex sequencing | |
33 | @cindex expression sequencing | |
34 | ||
35 | The @code{begin} syntax is used for grouping several expressions | |
36 | together so that they are treated as if they were one expression. | |
37 | This is particularly important when syntactic expressions are used | |
38 | which only allow one expression, but the programmer wants to use more | |
39 | than one expression in that place. As an example, consider the | |
40 | conditional expression below: | |
41 | ||
42 | @lisp | |
43 | (if (> x 0) | |
44 | (begin (display "greater") (newline))) | |
45 | @end lisp | |
46 | ||
47 | If the two calls to @code{display} and @code{newline} were not embedded | |
48 | in a @code{begin}-statement, the call to @code{newline} would get | |
49 | misinterpreted as the else-branch of the @code{if}-expression. | |
50 | ||
51 | @deffn syntax begin expr1 expr2 @dots{} | |
52 | The expression(s) are evaluated in left-to-right order and the value | |
53 | of the last expression is returned as the value of the | |
54 | @code{begin}-expression. This expression type is used when the | |
55 | expressions before the last one are evaluated for their side effects. | |
56 | ||
57 | Guile also allows the expression @code{(begin)}, a @code{begin} with no | |
58 | sub-expressions. Such an expression returns the `unspecified' value. | |
59 | @end deffn | |
60 | ||
61 | @node if cond case | |
62 | @subsection Simple Conditional Evaluation | |
63 | ||
64 | @cindex conditional evaluation | |
65 | @cindex if | |
66 | @cindex case | |
67 | @cindex cond | |
68 | ||
69 | Guile provides three syntactic constructs for conditional evaluation. | |
70 | @code{if} is the normal if-then-else expression (with an optional else | |
71 | branch), @code{cond} is a conditional expression with multiple branches | |
72 | and @code{case} branches if an expression has one of a set of constant | |
73 | values. | |
74 | ||
75 | @deffn syntax if test consequent [alternate] | |
76 | All arguments may be arbitrary expressions. First, @var{test} is | |
77 | evaluated. If it returns a true value, the expression @var{consequent} | |
78 | is evaluated and @var{alternate} is ignored. If @var{test} evaluates to | |
79 | @code{#f}, @var{alternate} is evaluated instead. The value of the | |
80 | evaluated branch (@var{consequent} or @var{alternate}) is returned as | |
81 | the value of the @code{if} expression. | |
82 | ||
83 | When @var{alternate} is omitted and the @var{test} evaluates to | |
84 | @code{#f}, the value of the expression is not specified. | |
85 | @end deffn | |
86 | ||
87 | @deffn syntax cond clause1 clause2 @dots{} | |
88 | Each @code{cond}-clause must look like this: | |
89 | ||
90 | @lisp | |
91 | (@var{test} @var{expression} @dots{}) | |
92 | @end lisp | |
93 | ||
94 | where @var{test} and @var{expression} are arbitrary expression, or like | |
95 | this | |
96 | ||
97 | @lisp | |
98 | (@var{test} => @var{expression}) | |
99 | @end lisp | |
100 | ||
101 | where @var{expression} must evaluate to a procedure. | |
102 | ||
103 | The @var{test}s of the clauses are evaluated in order and as soon as one | |
104 | of them evaluates to a true values, the corresponding @var{expression}s | |
105 | are evaluated in order and the last value is returned as the value of | |
106 | the @code{cond}-expression. For the @code{=>} clause type, | |
107 | @var{expression} is evaluated and the resulting procedure is applied to | |
108 | the value of @var{test}. The result of this procedure application is | |
109 | then the result of the @code{cond}-expression. | |
110 | ||
43ed3b69 MV |
111 | @cindex SRFI-61 |
112 | @cindex general cond clause | |
113 | @cindex multiple values and cond | |
114 | One additional @code{cond}-clause is available as an extension to | |
115 | standard Scheme: | |
116 | ||
117 | @lisp | |
118 | (@var{test} @var{guard} => @var{expression}) | |
119 | @end lisp | |
120 | ||
121 | where @var{guard} and @var{expression} must evaluate to procedures. | |
122 | For this clause type, @var{test} may return multiple values, and | |
123 | @code{cond} ignores its boolean state; instead, @code{cond} evaluates | |
124 | @var{guard} and applies the resulting procedure to the value(s) of | |
125 | @var{test}, as if @var{guard} were the @var{consumer} argument of | |
126 | @code{call-with-values}. Iff the result of that procedure call is a | |
127 | true value, it evaluates @var{expression} and applies the resulting | |
128 | procedure to the value(s) of @var{test}, in the same manner as the | |
129 | @var{guard} was called. | |
130 | ||
07d83abe MV |
131 | The @var{test} of the last @var{clause} may be the symbol @code{else}. |
132 | Then, if none of the preceding @var{test}s is true, the | |
133 | @var{expression}s following the @code{else} are evaluated to produce the | |
134 | result of the @code{cond}-expression. | |
135 | @end deffn | |
136 | ||
137 | @deffn syntax case key clause1 clause2 @dots{} | |
138 | @var{key} may be any expression, the @var{clause}s must have the form | |
139 | ||
140 | @lisp | |
141 | ((@var{datum1} @dots{}) @var{expr1} @var{expr2} @dots{}) | |
142 | @end lisp | |
143 | ||
144 | and the last @var{clause} may have the form | |
145 | ||
146 | @lisp | |
147 | (else @var{expr1} @var{expr2} @dots{}) | |
148 | @end lisp | |
149 | ||
150 | All @var{datum}s must be distinct. First, @var{key} is evaluated. The | |
ecb87335 | 151 | result of this evaluation is compared against all @var{datum} values using |
07d83abe MV |
152 | @code{eqv?}. When this comparison succeeds, the expression(s) following |
153 | the @var{datum} are evaluated from left to right, returning the value of | |
154 | the last expression as the result of the @code{case} expression. | |
155 | ||
156 | If the @var{key} matches no @var{datum} and there is an | |
157 | @code{else}-clause, the expressions following the @code{else} are | |
158 | evaluated. If there is no such clause, the result of the expression is | |
159 | unspecified. | |
160 | @end deffn | |
161 | ||
162 | ||
163 | @node and or | |
164 | @subsection Conditional Evaluation of a Sequence of Expressions | |
165 | ||
166 | @code{and} and @code{or} evaluate all their arguments in order, similar | |
167 | to @code{begin}, but evaluation stops as soon as one of the expressions | |
168 | evaluates to false or true, respectively. | |
169 | ||
170 | @deffn syntax and expr @dots{} | |
171 | Evaluate the @var{expr}s from left to right and stop evaluation as soon | |
172 | as one expression evaluates to @code{#f}; the remaining expressions are | |
173 | not evaluated. The value of the last evaluated expression is returned. | |
174 | If no expression evaluates to @code{#f}, the value of the last | |
175 | expression is returned. | |
176 | ||
177 | If used without expressions, @code{#t} is returned. | |
178 | @end deffn | |
179 | ||
180 | @deffn syntax or expr @dots{} | |
181 | Evaluate the @var{expr}s from left to right and stop evaluation as soon | |
182 | as one expression evaluates to a true value (that is, a value different | |
183 | from @code{#f}); the remaining expressions are not evaluated. The value | |
184 | of the last evaluated expression is returned. If all expressions | |
185 | evaluate to @code{#f}, @code{#f} is returned. | |
186 | ||
187 | If used without expressions, @code{#f} is returned. | |
188 | @end deffn | |
189 | ||
190 | ||
191 | @node while do | |
192 | @subsection Iteration mechanisms | |
193 | ||
194 | @cindex iteration | |
195 | @cindex looping | |
196 | @cindex named let | |
197 | ||
198 | Scheme has only few iteration mechanisms, mainly because iteration in | |
199 | Scheme programs is normally expressed using recursion. Nevertheless, | |
200 | R5RS defines a construct for programming loops, calling @code{do}. In | |
201 | addition, Guile has an explicit looping syntax called @code{while}. | |
202 | ||
203 | @deffn syntax do ((variable init [step]) @dots{}) (test [expr @dots{}]) body @dots{} | |
204 | Bind @var{variable}s and evaluate @var{body} until @var{test} is true. | |
205 | The return value is the last @var{expr} after @var{test}, if given. A | |
206 | simple example will illustrate the basic form, | |
207 | ||
208 | @example | |
209 | (do ((i 1 (1+ i))) | |
210 | ((> i 4)) | |
211 | (display i)) | |
212 | @print{} 1234 | |
213 | @end example | |
214 | ||
215 | @noindent | |
216 | Or with two variables and a final return value, | |
217 | ||
218 | @example | |
219 | (do ((i 1 (1+ i)) | |
220 | (p 3 (* 3 p))) | |
221 | ((> i 4) | |
222 | p) | |
223 | (format #t "3**~s is ~s\n" i p)) | |
224 | @print{} | |
225 | 3**1 is 3 | |
226 | 3**2 is 9 | |
227 | 3**3 is 27 | |
228 | 3**4 is 81 | |
229 | @result{} | |
230 | 789 | |
231 | @end example | |
232 | ||
233 | The @var{variable} bindings are established like a @code{let}, in that | |
234 | the expressions are all evaluated and then all bindings made. When | |
235 | iterating, the optional @var{step} expressions are evaluated with the | |
236 | previous bindings in scope, then new bindings all made. | |
237 | ||
238 | The @var{test} expression is a termination condition. Looping stops | |
239 | when the @var{test} is true. It's evaluated before running the | |
240 | @var{body} each time, so if it's true the first time then @var{body} | |
241 | is not run at all. | |
242 | ||
243 | The optional @var{expr}s after the @var{test} are evaluated at the end | |
244 | of looping, with the final @var{variable} bindings available. The | |
245 | last @var{expr} gives the return value, or if there are no @var{expr}s | |
246 | the return value is unspecified. | |
247 | ||
248 | Each iteration establishes bindings to fresh locations for the | |
249 | @var{variable}s, like a new @code{let} for each iteration. This is | |
250 | done for @var{variable}s without @var{step} expressions too. The | |
251 | following illustrates this, showing how a new @code{i} is captured by | |
252 | the @code{lambda} in each iteration (@pxref{About Closure,, The | |
253 | Concept of Closure}). | |
254 | ||
255 | @example | |
256 | (define lst '()) | |
257 | (do ((i 1 (1+ i))) | |
258 | ((> i 4)) | |
259 | (set! lst (cons (lambda () i) lst))) | |
260 | (map (lambda (proc) (proc)) lst) | |
261 | @result{} | |
262 | (4 3 2 1) | |
263 | @end example | |
264 | @end deffn | |
265 | ||
266 | @deffn syntax while cond body @dots{} | |
267 | Run a loop executing the @var{body} forms while @var{cond} is true. | |
268 | @var{cond} is tested at the start of each iteration, so if it's | |
91956a94 | 269 | @code{#f} the first time then @var{body} is not executed at all. |
07d83abe MV |
270 | |
271 | Within @code{while}, two extra bindings are provided, they can be used | |
272 | from both @var{cond} and @var{body}. | |
273 | ||
91956a94 | 274 | @deffn {Scheme Procedure} break break-arg... |
07d83abe MV |
275 | Break out of the @code{while} form. |
276 | @end deffn | |
277 | ||
278 | @deffn {Scheme Procedure} continue | |
279 | Abandon the current iteration, go back to the start and test | |
280 | @var{cond} again, etc. | |
281 | @end deffn | |
282 | ||
91956a94 AW |
283 | If the loop terminates normally, by the @var{cond} evaluating to |
284 | @code{#f}, then the @code{while} expression as a whole evaluates to | |
285 | @code{#f}. If it terminates by a call to @code{break} with some number | |
286 | of arguments, those arguments are returned from the @code{while} | |
287 | expression, as multiple values. Otherwise if it terminates by a call to | |
288 | @code{break} with no arguments, then return value is @code{#t}. | |
289 | ||
290 | @example | |
291 | (while #f (error "not reached")) @result{} #f | |
292 | (while #t (break)) @result{} #t | |
80069014 | 293 | (while #t (break 1 2 3)) @result{} 1 2 3 |
91956a94 AW |
294 | @end example |
295 | ||
07d83abe MV |
296 | Each @code{while} form gets its own @code{break} and @code{continue} |
297 | procedures, operating on that @code{while}. This means when loops are | |
298 | nested the outer @code{break} can be used to escape all the way out. | |
299 | For example, | |
300 | ||
301 | @example | |
302 | (while (test1) | |
303 | (let ((outer-break break)) | |
304 | (while (test2) | |
305 | (if (something) | |
306 | (outer-break #f)) | |
307 | ...))) | |
308 | @end example | |
309 | ||
310 | Note that each @code{break} and @code{continue} procedure can only be | |
311 | used within the dynamic extent of its @code{while}. Outside the | |
312 | @code{while} their behaviour is unspecified. | |
313 | @end deffn | |
314 | ||
315 | @cindex named let | |
316 | Another very common way of expressing iteration in Scheme programs is | |
317 | the use of the so-called @dfn{named let}. | |
318 | ||
319 | Named let is a variant of @code{let} which creates a procedure and calls | |
320 | it in one step. Because of the newly created procedure, named let is | |
321 | more powerful than @code{do}--it can be used for iteration, but also | |
322 | for arbitrary recursion. | |
323 | ||
324 | @deffn syntax let variable bindings body | |
325 | For the definition of @var{bindings} see the documentation about | |
326 | @code{let} (@pxref{Local Bindings}). | |
327 | ||
328 | Named @code{let} works as follows: | |
329 | ||
330 | @itemize @bullet | |
331 | @item | |
332 | A new procedure which accepts as many arguments as are in @var{bindings} | |
333 | is created and bound locally (using @code{let}) to @var{variable}. The | |
334 | new procedure's formal argument names are the name of the | |
335 | @var{variables}. | |
336 | ||
337 | @item | |
338 | The @var{body} expressions are inserted into the newly created procedure. | |
339 | ||
340 | @item | |
341 | The procedure is called with the @var{init} expressions as the formal | |
342 | arguments. | |
343 | @end itemize | |
344 | ||
345 | The next example implements a loop which iterates (by recursion) 1000 | |
346 | times. | |
347 | ||
348 | @lisp | |
349 | (let lp ((x 1000)) | |
350 | (if (positive? x) | |
351 | (lp (- x 1)) | |
352 | x)) | |
353 | @result{} | |
354 | 0 | |
355 | @end lisp | |
356 | @end deffn | |
357 | ||
358 | ||
17ed90df AW |
359 | @node Prompts |
360 | @subsection Prompts | |
361 | @cindex prompts | |
362 | @cindex delimited continuations | |
363 | @cindex composable continuations | |
364 | @cindex non-local exit | |
365 | ||
366 | Prompts are control-flow barriers between different parts of a program. In the | |
367 | same way that a user sees a shell prompt (e.g., the Bash prompt) as a barrier | |
368 | between the operating system and her programs, Scheme prompts allow the Scheme | |
369 | programmer to treat parts of programs as if they were running in different | |
370 | operating systems. | |
371 | ||
372 | We use this roundabout explanation because, unless you're a functional | |
373 | programming junkie, you probably haven't heard the term, ``delimited, composable | |
374 | continuation''. That's OK; it's a relatively recent topic, but a very useful | |
375 | one to know about. | |
376 | ||
377 | @deffn {Scheme Procedure} call-with-prompt tag thunk handler | |
378 | Set up a prompt, and call @var{thunk} within that prompt. | |
379 | ||
380 | During the dynamic extent of the call to @var{thunk}, a prompt named @var{tag} | |
381 | will be present in the dynamic context, such that if a user calls | |
382 | @code{abort-to-prompt} (see below) with that tag, control rewinds back to the | |
383 | prompt, and the @var{handler} is run. | |
384 | ||
385 | @var{handler} must be a procedure. The first argument to @var{handler} will be | |
386 | the state of the computation begun when @var{thunk} was called, and ending with | |
387 | the call to @code{abort-to-prompt}. The remaining arguments to @var{handler} are | |
388 | those passed to @code{abort-to-prompt}. | |
389 | @end deffn | |
390 | ||
391 | @deffn {Scheme Procedure} abort-to-prompt tag val ... | |
392 | Unwind the dynamic and control context to the nearest prompt named @var{tag}, | |
393 | also passing the given values. | |
394 | @end deffn | |
395 | ||
396 | C programmers may recognize @code{call-with-prompt} and @code{abort-to-prompt} | |
397 | as a fancy kind of @code{setjmp} and @code{longjmp}, respectively. Prompts are | |
398 | indeed quite useful as non-local escape mechanisms. Guile's @code{catch} and | |
399 | @code{throw} are implemented in terms of prompts. Prompts are more convenient | |
400 | than @code{longjmp}, in that one has the opportunity to pass multiple values to | |
401 | the jump target. | |
402 | ||
403 | Also unlike @code{longjmp}, the prompt handler is given the full state of the | |
404 | process that was aborted, as the first argument to the prompt's handler. That | |
405 | state is the @dfn{continuation} of the computation wrapped by the prompt. It is | |
406 | a @dfn{delimited continuation}, because it is not the whole continuation of the | |
407 | program; rather, just the computation initiated by the call to | |
408 | @code{call-with-prompt}. | |
409 | ||
410 | The continuation is a procedure, and may be reinstated simply by invoking it, | |
411 | with any number of values. Here's where things get interesting, and complicated | |
412 | as well. Besides being described as delimited, continuations reified by prompts | |
413 | are also @dfn{composable}, because invoking a prompt-saved continuation composes | |
414 | that continuation with the current one. | |
415 | ||
416 | Imagine you have saved a continuation via call-with-prompt: | |
417 | ||
418 | @example | |
419 | (define cont | |
420 | (call-with-prompt | |
421 | ;; tag | |
422 | 'foo | |
423 | ;; thunk | |
424 | (lambda () | |
425 | (+ 34 (abort-to-prompt 'foo))) | |
426 | ;; handler | |
427 | (lambda (k) k))) | |
428 | @end example | |
429 | ||
430 | The resulting continuation is the addition of 34. It's as if you had written: | |
431 | ||
432 | @example | |
433 | (define cont | |
434 | (lambda (x) | |
435 | (+ 34 x))) | |
436 | @end example | |
437 | ||
438 | So, if we call @code{cont} with one numeric value, we get that number, | |
439 | incremented by 34: | |
440 | ||
441 | @example | |
442 | (cont 8) | |
443 | @result{} 42 | |
444 | (* 2 (cont 8)) | |
445 | @result{} 84 | |
446 | @end example | |
447 | ||
448 | The last example illustrates what we mean when we say, "composes with the | |
449 | current continuation". We mean that there is a current continuation -- some | |
450 | remaining things to compute, like @code{(lambda (x) (* x 2))} -- and that | |
451 | calling the saved continuation doesn't wipe out the current continuation, it | |
452 | composes the saved continuation with the current one. | |
453 | ||
454 | We're belaboring the point here because traditional Scheme continuations, as | |
455 | discussed in the next section, aren't composable, and are actually less | |
456 | expressive than continuations captured by prompts. But there's a place for them | |
457 | both. | |
458 | ||
459 | Before moving on, we should mention that if the handler of a prompt is a | |
460 | @code{lambda} expression, and the first argument isn't referenced, an abort to | |
461 | that prompt will not cause a continuation to be reified. This can be an | |
462 | important efficiency consideration to keep in mind. | |
463 | ||
464 | ||
07d83abe MV |
465 | @node Continuations |
466 | @subsection Continuations | |
467 | @cindex continuations | |
468 | ||
469 | A ``continuation'' is the code that will execute when a given function | |
470 | or expression returns. For example, consider | |
471 | ||
472 | @example | |
473 | (define (foo) | |
474 | (display "hello\n") | |
475 | (display (bar)) (newline) | |
476 | (exit)) | |
477 | @end example | |
478 | ||
479 | The continuation from the call to @code{bar} comprises a | |
480 | @code{display} of the value returned, a @code{newline} and an | |
481 | @code{exit}. This can be expressed as a function of one argument. | |
482 | ||
483 | @example | |
484 | (lambda (r) | |
485 | (display r) (newline) | |
486 | (exit)) | |
487 | @end example | |
488 | ||
489 | In Scheme, continuations are represented as special procedures just | |
490 | like this. The special property is that when a continuation is called | |
491 | it abandons the current program location and jumps directly to that | |
492 | represented by the continuation. | |
493 | ||
494 | A continuation is like a dynamic label, capturing at run-time a point | |
495 | in program execution, including all the nested calls that have lead to | |
496 | it (or rather the code that will execute when those calls return). | |
497 | ||
498 | Continuations are created with the following functions. | |
499 | ||
500 | @deffn {Scheme Procedure} call-with-current-continuation proc | |
501 | @deffnx {Scheme Procedure} call/cc proc | |
502 | @rnindex call-with-current-continuation | |
503 | Capture the current continuation and call @code{(@var{proc} | |
504 | @var{cont})} with it. The return value is the value returned by | |
505 | @var{proc}, or when @code{(@var{cont} @var{value})} is later invoked, | |
506 | the return is the @var{value} passed. | |
507 | ||
508 | Normally @var{cont} should be called with one argument, but when the | |
509 | location resumed is expecting multiple values (@pxref{Multiple | |
510 | Values}) then they should be passed as multiple arguments, for | |
511 | instance @code{(@var{cont} @var{x} @var{y} @var{z})}. | |
512 | ||
b4fddbbe MV |
513 | @var{cont} may only be used from the same side of a continuation |
514 | barrier as it was created (@pxref{Continuation Barriers}), and in a | |
515 | multi-threaded program only from the thread in which it was created. | |
07d83abe MV |
516 | |
517 | The call to @var{proc} is not part of the continuation captured, it runs | |
518 | only when the continuation is created. Often a program will want to | |
519 | store @var{cont} somewhere for later use; this can be done in | |
520 | @var{proc}. | |
521 | ||
522 | The @code{call} in the name @code{call-with-current-continuation} | |
523 | refers to the way a call to @var{proc} gives the newly created | |
524 | continuation. It's not related to the way a call is used later to | |
525 | invoke that continuation. | |
526 | ||
527 | @code{call/cc} is an alias for @code{call-with-current-continuation}. | |
528 | This is in common use since the latter is rather long. | |
529 | @end deffn | |
530 | ||
07d83abe MV |
531 | @sp 1 |
532 | @noindent | |
533 | Here is a simple example, | |
534 | ||
535 | @example | |
536 | (define kont #f) | |
537 | (format #t "the return is ~a\n" | |
538 | (call/cc (lambda (k) | |
539 | (set! kont k) | |
540 | 1))) | |
541 | @result{} the return is 1 | |
542 | ||
543 | (kont 2) | |
544 | @result{} the return is 2 | |
545 | @end example | |
546 | ||
547 | @code{call/cc} captures a continuation in which the value returned is | |
548 | going to be displayed by @code{format}. The @code{lambda} stores this | |
549 | in @code{kont} and gives an initial return @code{1} which is | |
550 | displayed. The later invocation of @code{kont} resumes the captured | |
551 | point, but this time returning @code{2}, which is displayed. | |
552 | ||
553 | When Guile is run interactively, a call to @code{format} like this has | |
554 | an implicit return back to the read-eval-print loop. @code{call/cc} | |
555 | captures that like any other return, which is why interactively | |
556 | @code{kont} will come back to read more input. | |
557 | ||
558 | @sp 1 | |
559 | C programmers may note that @code{call/cc} is like @code{setjmp} in | |
560 | the way it records at runtime a point in program execution. A call to | |
561 | a continuation is like a @code{longjmp} in that it abandons the | |
562 | present location and goes to the recorded one. Like @code{longjmp}, | |
563 | the value passed to the continuation is the value returned by | |
564 | @code{call/cc} on resuming there. However @code{longjmp} can only go | |
565 | up the program stack, but the continuation mechanism can go anywhere. | |
566 | ||
567 | When a continuation is invoked, @code{call/cc} and subsequent code | |
568 | effectively ``returns'' a second time. It can be confusing to imagine | |
569 | a function returning more times than it was called. It may help | |
570 | instead to think of it being stealthily re-entered and then program | |
571 | flow going on as normal. | |
572 | ||
573 | @code{dynamic-wind} (@pxref{Dynamic Wind}) can be used to ensure setup | |
574 | and cleanup code is run when a program locus is resumed or abandoned | |
661ae7ab | 575 | through the continuation mechanism. |
07d83abe MV |
576 | |
577 | @sp 1 | |
578 | Continuations are a powerful mechanism, and can be used to implement | |
579 | almost any sort of control structure, such as loops, coroutines, or | |
580 | exception handlers. | |
581 | ||
582 | However the implementation of continuations in Guile is not as | |
583 | efficient as one might hope, because Guile is designed to cooperate | |
584 | with programs written in other languages, such as C, which do not know | |
585 | about continuations. Basically continuations are captured by a block | |
586 | copy of the stack, and resumed by copying back. | |
587 | ||
17ed90df AW |
588 | For this reason, continuations captured by @code{call/cc} should be used only |
589 | when there is no other simple way to achieve the desired result, or when the | |
590 | elegance of the continuation mechanism outweighs the need for performance. | |
07d83abe MV |
591 | |
592 | Escapes upwards from loops or nested functions are generally best | |
17ed90df | 593 | handled with prompts (@pxref{Prompts}). Coroutines can be |
07d83abe MV |
594 | efficiently implemented with cooperating threads (a thread holds a |
595 | full program stack but doesn't copy it around the way continuations | |
596 | do). | |
597 | ||
598 | ||
599 | @node Multiple Values | |
600 | @subsection Returning and Accepting Multiple Values | |
601 | ||
602 | @cindex multiple values | |
603 | @cindex receive | |
604 | ||
605 | Scheme allows a procedure to return more than one value to its caller. | |
606 | This is quite different to other languages which only allow | |
607 | single-value returns. Returning multiple values is different from | |
608 | returning a list (or pair or vector) of values to the caller, because | |
609 | conceptually not @emph{one} compound object is returned, but several | |
610 | distinct values. | |
611 | ||
612 | The primitive procedures for handling multiple values are @code{values} | |
613 | and @code{call-with-values}. @code{values} is used for returning | |
614 | multiple values from a procedure. This is done by placing a call to | |
615 | @code{values} with zero or more arguments in tail position in a | |
616 | procedure body. @code{call-with-values} combines a procedure returning | |
617 | multiple values with a procedure which accepts these values as | |
618 | parameters. | |
619 | ||
620 | @rnindex values | |
621 | @deffn {Scheme Procedure} values arg1 @dots{} argN | |
622 | @deffnx {C Function} scm_values (args) | |
623 | Delivers all of its arguments to its continuation. Except for | |
624 | continuations created by the @code{call-with-values} procedure, | |
625 | all continuations take exactly one value. The effect of | |
626 | passing no value or more than one value to continuations that | |
627 | were not created by @code{call-with-values} is unspecified. | |
628 | ||
629 | For @code{scm_values}, @var{args} is a list of arguments and the | |
630 | return is a multiple-values object which the caller can return. In | |
631 | the current implementation that object shares structure with | |
632 | @var{args}, so @var{args} should not be modified subsequently. | |
633 | @end deffn | |
634 | ||
635 | @rnindex call-with-values | |
636 | @deffn {Scheme Procedure} call-with-values producer consumer | |
637 | Calls its @var{producer} argument with no values and a | |
638 | continuation that, when passed some values, calls the | |
639 | @var{consumer} procedure with those values as arguments. The | |
640 | continuation for the call to @var{consumer} is the continuation | |
641 | of the call to @code{call-with-values}. | |
642 | ||
643 | @example | |
644 | (call-with-values (lambda () (values 4 5)) | |
645 | (lambda (a b) b)) | |
646 | @result{} 5 | |
647 | ||
648 | @end example | |
649 | @example | |
650 | (call-with-values * -) | |
651 | @result{} -1 | |
652 | @end example | |
653 | @end deffn | |
654 | ||
655 | In addition to the fundamental procedures described above, Guile has a | |
23f2b9a3 KR |
656 | module which exports a syntax called @code{receive}, which is much |
657 | more convenient. This is in the @code{(ice-9 receive)} and is the | |
658 | same as specified by SRFI-8 (@pxref{SRFI-8}). | |
07d83abe MV |
659 | |
660 | @lisp | |
661 | (use-modules (ice-9 receive)) | |
662 | @end lisp | |
663 | ||
664 | @deffn {library syntax} receive formals expr body @dots{} | |
23f2b9a3 KR |
665 | Evaluate the expression @var{expr}, and bind the result values (zero |
666 | or more) to the formal arguments in @var{formals}. @var{formals} is a | |
667 | list of symbols, like the argument list in a @code{lambda} | |
668 | (@pxref{Lambda}). After binding the variables, the expressions in | |
669 | @var{body} @dots{} are evaluated in order, the return value is the | |
670 | result from the last expression. | |
671 | ||
672 | For example getting results from @code{partition} in SRFI-1 | |
673 | (@pxref{SRFI-1}), | |
674 | ||
675 | @example | |
676 | (receive (odds evens) | |
677 | (partition odd? '(7 4 2 8 3)) | |
678 | (display odds) | |
679 | (display " and ") | |
680 | (display evens)) | |
681 | @print{} (7 3) and (4 2 8) | |
682 | @end example | |
683 | ||
07d83abe MV |
684 | @end deffn |
685 | ||
686 | ||
687 | @node Exceptions | |
688 | @subsection Exceptions | |
689 | @cindex error handling | |
690 | @cindex exception handling | |
691 | ||
692 | A common requirement in applications is to want to jump | |
693 | @dfn{non-locally} from the depths of a computation back to, say, the | |
694 | application's main processing loop. Usually, the place that is the | |
695 | target of the jump is somewhere in the calling stack of procedures that | |
696 | called the procedure that wants to jump back. For example, typical | |
697 | logic for a key press driven application might look something like this: | |
698 | ||
699 | @example | |
700 | main-loop: | |
701 | read the next key press and call dispatch-key | |
702 | ||
703 | dispatch-key: | |
704 | lookup the key in a keymap and call an appropriate procedure, | |
705 | say find-file | |
706 | ||
707 | find-file: | |
708 | interactively read the required file name, then call | |
709 | find-specified-file | |
710 | ||
711 | find-specified-file: | |
712 | check whether file exists; if not, jump back to main-loop | |
713 | @dots{} | |
714 | @end example | |
715 | ||
716 | The jump back to @code{main-loop} could be achieved by returning through | |
717 | the stack one procedure at a time, using the return value of each | |
718 | procedure to indicate the error condition, but Guile (like most modern | |
719 | programming languages) provides an additional mechanism called | |
720 | @dfn{exception handling} that can be used to implement such jumps much | |
721 | more conveniently. | |
722 | ||
723 | @menu | |
724 | * Exception Terminology:: Different ways to say the same thing. | |
725 | * Catch:: Setting up to catch exceptions. | |
e10cf6b9 | 726 | * Throw Handlers:: Handling exceptions before unwinding the stack. |
7b4c914e | 727 | * Throw:: Throwing an exception. |
07d83abe MV |
728 | * Exception Implementation:: How Guile implements exceptions. |
729 | @end menu | |
730 | ||
731 | ||
732 | @node Exception Terminology | |
733 | @subsubsection Exception Terminology | |
734 | ||
735 | There are several variations on the terminology for dealing with | |
736 | non-local jumps. It is useful to be aware of them, and to realize | |
737 | that they all refer to the same basic mechanism. | |
738 | ||
739 | @itemize @bullet | |
740 | @item | |
741 | Actually making a non-local jump may be called @dfn{raising an | |
742 | exception}, @dfn{raising a signal}, @dfn{throwing an exception} or | |
743 | @dfn{doing a long jump}. When the jump indicates an error condition, | |
744 | people may talk about @dfn{signalling}, @dfn{raising} or @dfn{throwing} | |
745 | @dfn{an error}. | |
746 | ||
747 | @item | |
748 | Handling the jump at its target may be referred to as @dfn{catching} or | |
749 | @dfn{handling} the @dfn{exception}, @dfn{signal} or, where an error | |
750 | condition is involved, @dfn{error}. | |
751 | @end itemize | |
752 | ||
753 | Where @dfn{signal} and @dfn{signalling} are used, special care is needed | |
754 | to avoid the risk of confusion with POSIX signals. | |
755 | ||
756 | This manual prefers to speak of throwing and catching exceptions, since | |
757 | this terminology matches the corresponding Guile primitives. | |
758 | ||
759 | ||
760 | @node Catch | |
761 | @subsubsection Catching Exceptions | |
762 | ||
763 | @code{catch} is used to set up a target for a possible non-local jump. | |
764 | The arguments of a @code{catch} expression are a @dfn{key}, which | |
765 | restricts the set of exceptions to which this @code{catch} applies, a | |
7b4c914e NJ |
766 | thunk that specifies the code to execute and one or two @dfn{handler} |
767 | procedures that say what to do if an exception is thrown while executing | |
768 | the code. If the execution thunk executes @dfn{normally}, which means | |
769 | without throwing any exceptions, the handler procedures are not called | |
770 | at all. | |
07d83abe MV |
771 | |
772 | When an exception is thrown using the @code{throw} function, the first | |
773 | argument of the @code{throw} is a symbol that indicates the type of the | |
774 | exception. For example, Guile throws an exception using the symbol | |
775 | @code{numerical-overflow} to indicate numerical overflow errors such as | |
776 | division by zero: | |
777 | ||
778 | @lisp | |
779 | (/ 1 0) | |
780 | @result{} | |
781 | ABORT: (numerical-overflow) | |
782 | @end lisp | |
783 | ||
784 | The @var{key} argument in a @code{catch} expression corresponds to this | |
785 | symbol. @var{key} may be a specific symbol, such as | |
786 | @code{numerical-overflow}, in which case the @code{catch} applies | |
787 | specifically to exceptions of that type; or it may be @code{#t}, which | |
788 | means that the @code{catch} applies to all exceptions, irrespective of | |
789 | their type. | |
790 | ||
791 | The second argument of a @code{catch} expression should be a thunk | |
679cceed | 792 | (i.e.@: a procedure that accepts no arguments) that specifies the normal |
07d83abe MV |
793 | case code. The @code{catch} is active for the execution of this thunk, |
794 | including any code called directly or indirectly by the thunk's body. | |
795 | Evaluation of the @code{catch} expression activates the catch and then | |
796 | calls this thunk. | |
797 | ||
798 | The third argument of a @code{catch} expression is a handler procedure. | |
799 | If an exception is thrown, this procedure is called with exactly the | |
800 | arguments specified by the @code{throw}. Therefore, the handler | |
801 | procedure must be designed to accept a number of arguments that | |
802 | corresponds to the number of arguments in all @code{throw} expressions | |
803 | that can be caught by this @code{catch}. | |
804 | ||
7b4c914e NJ |
805 | The fourth, optional argument of a @code{catch} expression is another |
806 | handler procedure, called the @dfn{pre-unwind} handler. It differs from | |
807 | the third argument in that if an exception is thrown, it is called, | |
808 | @emph{before} the third argument handler, in exactly the dynamic context | |
809 | of the @code{throw} expression that threw the exception. This means | |
810 | that it is useful for capturing or displaying the stack at the point of | |
811 | the @code{throw}, or for examining other aspects of the dynamic context, | |
812 | such as fluid values, before the context is unwound back to that of the | |
813 | prevailing @code{catch}. | |
814 | ||
815 | @deffn {Scheme Procedure} catch key thunk handler [pre-unwind-handler] | |
816 | @deffnx {C Function} scm_catch_with_pre_unwind_handler (key, thunk, handler, pre_unwind_handler) | |
07d83abe MV |
817 | @deffnx {C Function} scm_catch (key, thunk, handler) |
818 | Invoke @var{thunk} in the dynamic context of @var{handler} for | |
819 | exceptions matching @var{key}. If thunk throws to the symbol | |
820 | @var{key}, then @var{handler} is invoked this way: | |
821 | @lisp | |
822 | (handler key args ...) | |
823 | @end lisp | |
824 | ||
825 | @var{key} is a symbol or @code{#t}. | |
826 | ||
827 | @var{thunk} takes no arguments. If @var{thunk} returns | |
828 | normally, that is the return value of @code{catch}. | |
829 | ||
830 | Handler is invoked outside the scope of its own @code{catch}. | |
831 | If @var{handler} again throws to the same key, a new handler | |
832 | from further up the call chain is invoked. | |
833 | ||
834 | If the key is @code{#t}, then a throw to @emph{any} symbol will | |
835 | match this call to @code{catch}. | |
7b4c914e NJ |
836 | |
837 | If a @var{pre-unwind-handler} is given and @var{thunk} throws | |
838 | an exception that matches @var{key}, Guile calls the | |
839 | @var{pre-unwind-handler} before unwinding the dynamic state and | |
840 | invoking the main @var{handler}. @var{pre-unwind-handler} should | |
841 | be a procedure with the same signature as @var{handler}, that | |
842 | is @code{(lambda (key . args))}. It is typically used to save | |
843 | the stack at the point where the exception occurred, but can also | |
844 | query other parts of the dynamic state at that point, such as | |
845 | fluid values. | |
846 | ||
847 | A @var{pre-unwind-handler} can exit either normally or non-locally. | |
848 | If it exits normally, Guile unwinds the stack and dynamic context | |
849 | and then calls the normal (third argument) handler. If it exits | |
850 | non-locally, that exit determines the continuation. | |
07d83abe MV |
851 | @end deffn |
852 | ||
7b4c914e | 853 | If a handler procedure needs to match a variety of @code{throw} |
07d83abe MV |
854 | expressions with varying numbers of arguments, you should write it like |
855 | this: | |
856 | ||
857 | @lisp | |
858 | (lambda (key . args) | |
859 | @dots{}) | |
860 | @end lisp | |
861 | ||
862 | @noindent | |
863 | The @var{key} argument is guaranteed always to be present, because a | |
864 | @code{throw} without a @var{key} is not valid. The number and | |
865 | interpretation of the @var{args} varies from one type of exception to | |
866 | another, but should be specified by the documentation for each exception | |
867 | type. | |
868 | ||
7b4c914e NJ |
869 | Note that, once the normal (post-unwind) handler procedure is invoked, |
870 | the catch that led to the handler procedure being called is no longer | |
871 | active. Therefore, if the handler procedure itself throws an exception, | |
872 | that exception can only be caught by another active catch higher up the | |
873 | call stack, if there is one. | |
07d83abe MV |
874 | |
875 | @sp 1 | |
7b4c914e NJ |
876 | @deftypefn {C Function} SCM scm_c_catch (SCM tag, scm_t_catch_body body, void *body_data, scm_t_catch_handler handler, void *handler_data, scm_t_catch_handler pre_unwind_handler, void *pre_unwind_handler_data) |
877 | @deftypefnx {C Function} SCM scm_internal_catch (SCM tag, scm_t_catch_body body, void *body_data, scm_t_catch_handler handler, void *handler_data) | |
878 | The above @code{scm_catch_with_pre_unwind_handler} and @code{scm_catch} | |
879 | take Scheme procedures as body and handler arguments. | |
880 | @code{scm_c_catch} and @code{scm_internal_catch} are equivalents taking | |
881 | C functions. | |
882 | ||
883 | @var{body} is called as @code{@var{body} (@var{body_data})} with a catch | |
884 | on exceptions of the given @var{tag} type. If an exception is caught, | |
885 | @var{pre_unwind_handler} and @var{handler} are called as | |
886 | @code{@var{handler} (@var{handler_data}, @var{key}, @var{args})}. | |
887 | @var{key} and @var{args} are the @code{SCM} key and argument list from | |
888 | the @code{throw}. | |
07d83abe MV |
889 | |
890 | @tpindex scm_t_catch_body | |
891 | @tpindex scm_t_catch_handler | |
892 | @var{body} and @var{handler} should have the following prototypes. | |
893 | @code{scm_t_catch_body} and @code{scm_t_catch_handler} are pointer | |
894 | typedefs for these. | |
895 | ||
896 | @example | |
897 | SCM body (void *data); | |
898 | SCM handler (void *data, SCM key, SCM args); | |
899 | @end example | |
900 | ||
901 | The @var{body_data} and @var{handler_data} parameters are passed to | |
902 | the respective calls so an application can communicate extra | |
903 | information to those functions. | |
904 | ||
905 | If the data consists of an @code{SCM} object, care should be taken | |
906 | that it isn't garbage collected while still required. If the | |
907 | @code{SCM} is a local C variable, one way to protect it is to pass a | |
908 | pointer to that variable as the data parameter, since the C compiler | |
909 | will then know the value must be held on the stack. Another way is to | |
910 | use @code{scm_remember_upto_here_1} (@pxref{Remembering During | |
911 | Operations}). | |
912 | @end deftypefn | |
913 | ||
914 | ||
7b4c914e NJ |
915 | @node Throw Handlers |
916 | @subsubsection Throw Handlers | |
07d83abe | 917 | |
7b4c914e | 918 | It's sometimes useful to be able to intercept an exception that is being |
e10cf6b9 AW |
919 | thrown before the stack is unwound. This could be to clean up some |
920 | related state, to print a backtrace, or to pass information about the | |
921 | exception to a debugger, for example. The @code{with-throw-handler} | |
922 | procedure provides a way to do this. | |
07d83abe | 923 | |
7b4c914e NJ |
924 | @deffn {Scheme Procedure} with-throw-handler key thunk handler |
925 | @deffnx {C Function} scm_with_throw_handler (key, thunk, handler) | |
926 | Add @var{handler} to the dynamic context as a throw handler | |
927 | for key @var{key}, then invoke @var{thunk}. | |
e10cf6b9 AW |
928 | |
929 | This behaves exactly like @code{catch}, except that it does not unwind | |
930 | the stack before invoking @var{handler}. If the @var{handler} procedure | |
931 | returns normally, Guile rethrows the same exception again to the next | |
932 | innermost catch or throw handler. @var{handler} may exit nonlocally, of | |
933 | course, via an explicit throw or via invoking a continuation. | |
07d83abe MV |
934 | @end deffn |
935 | ||
e10cf6b9 AW |
936 | Typically @var{handler} is used to display a backtrace of the stack at |
937 | the point where the corresponding @code{throw} occurred, or to save off | |
938 | this information for possible display later. | |
939 | ||
940 | Not unwinding the stack means that throwing an exception that is handled | |
941 | via a throw handler is equivalent to calling the throw handler handler | |
942 | inline instead of each @code{throw}, and then omitting the surrounding | |
943 | @code{with-throw-handler}. In other words, | |
944 | ||
945 | @lisp | |
946 | (with-throw-handler 'key | |
947 | (lambda () @dots{} (throw 'key args @dots{}) @dots{}) | |
948 | handler) | |
949 | @end lisp | |
950 | ||
951 | @noindent | |
952 | is mostly equivalent to | |
953 | ||
954 | @lisp | |
955 | ((lambda () @dots{} (handler 'key args @dots{}) @dots{})) | |
956 | @end lisp | |
957 | ||
958 | In particular, the dynamic context when @var{handler} is invoked is that | |
959 | of the site where @code{throw} is called. The examples are not quite | |
960 | equivalent, because the body of a @code{with-throw-handler} is not in | |
961 | tail position with respect to the @code{with-throw-handler}, and if | |
962 | @var{handler} exits normally, Guile arranges to rethrow the error, but | |
963 | hopefully the intention is clear. (For an introduction to what is meant | |
964 | by dynamic context, @xref{Dynamic Wind}.) | |
965 | ||
7b4c914e NJ |
966 | @deftypefn {C Function} SCM scm_c_with_throw_handler (SCM tag, scm_t_catch_body body, void *body_data, scm_t_catch_handler handler, void *handler_data, int lazy_catch_p) |
967 | The above @code{scm_with_throw_handler} takes Scheme procedures as body | |
968 | (thunk) and handler arguments. @code{scm_c_with_throw_handler} is an | |
969 | equivalent taking C functions. See @code{scm_c_catch} (@pxref{Catch}) | |
970 | for a description of the parameters, the behaviour however of course | |
971 | follows @code{with-throw-handler}. | |
972 | @end deftypefn | |
07d83abe | 973 | |
7b4c914e NJ |
974 | If @var{thunk} throws an exception, Guile handles that exception by |
975 | invoking the innermost @code{catch} or throw handler whose key matches | |
976 | that of the exception. When the innermost thing is a throw handler, | |
977 | Guile calls the specified handler procedure using @code{(apply | |
978 | @var{handler} key args)}. The handler procedure may either return | |
979 | normally or exit non-locally. If it returns normally, Guile passes the | |
980 | exception on to the next innermost @code{catch} or throw handler. If it | |
981 | exits non-locally, that exit determines the continuation. | |
982 | ||
983 | The behaviour of a throw handler is very similar to that of a | |
984 | @code{catch} expression's optional pre-unwind handler. In particular, a | |
985 | throw handler's handler procedure is invoked in the exact dynamic | |
986 | context of the @code{throw} expression, just as a pre-unwind handler is. | |
987 | @code{with-throw-handler} may be seen as a half-@code{catch}: it does | |
988 | everything that a @code{catch} would do until the point where | |
989 | @code{catch} would start unwinding the stack and dynamic context, but | |
990 | then it rethrows to the next innermost @code{catch} or throw handler | |
991 | instead. | |
07d83abe | 992 | |
e10cf6b9 AW |
993 | Note also that since the dynamic context is not unwound, if a |
994 | @code{with-throw-handler} handler throws to a key that does not match | |
995 | the @code{with-throw-handler} expression's @var{key}, the new throw may | |
996 | be handled by a @code{catch} or throw handler that is @emph{closer} to | |
997 | the throw than the first @code{with-throw-handler}. | |
07d83abe | 998 | |
e10cf6b9 | 999 | Here is an example to illustrate this behavior: |
7b4c914e NJ |
1000 | |
1001 | @lisp | |
1002 | (catch 'a | |
1003 | (lambda () | |
1004 | (with-throw-handler 'b | |
1005 | (lambda () | |
1006 | (catch 'a | |
1007 | (lambda () | |
1008 | (throw 'b)) | |
1009 | inner-handler)) | |
1010 | (lambda (key . args) | |
1011 | (throw 'a)))) | |
1012 | outer-handler) | |
1013 | @end lisp | |
1014 | ||
1015 | @noindent | |
1016 | This code will call @code{inner-handler} and then continue with the | |
e10cf6b9 | 1017 | continuation of the inner @code{catch}. |
7b4c914e NJ |
1018 | |
1019 | ||
1020 | @node Throw | |
1021 | @subsubsection Throwing Exceptions | |
1022 | ||
1023 | The @code{throw} primitive is used to throw an exception. One argument, | |
1024 | the @var{key}, is mandatory, and must be a symbol; it indicates the type | |
1025 | of exception that is being thrown. Following the @var{key}, | |
1026 | @code{throw} accepts any number of additional arguments, whose meaning | |
1027 | depends on the exception type. The documentation for each possible type | |
1028 | of exception should specify the additional arguments that are expected | |
1029 | for that kind of exception. | |
1030 | ||
1031 | @deffn {Scheme Procedure} throw key . args | |
1032 | @deffnx {C Function} scm_throw (key, args) | |
1033 | Invoke the catch form matching @var{key}, passing @var{args} to the | |
1034 | @var{handler}. | |
1035 | ||
1036 | @var{key} is a symbol. It will match catches of the same symbol or of | |
1037 | @code{#t}. | |
1038 | ||
1039 | If there is no handler at all, Guile prints an error and then exits. | |
1040 | @end deffn | |
1041 | ||
1042 | When an exception is thrown, it will be caught by the innermost | |
1043 | @code{catch} or throw handler that applies to the type of the thrown | |
1044 | exception; in other words, whose @var{key} is either @code{#t} or the | |
1045 | same symbol as that used in the @code{throw} expression. Once Guile has | |
1046 | identified the appropriate @code{catch} or throw handler, it handles the | |
1047 | exception by applying the relevant handler procedure(s) to the arguments | |
1048 | of the @code{throw}. | |
1049 | ||
1050 | If there is no appropriate @code{catch} or throw handler for a thrown | |
1051 | exception, Guile prints an error to the current error port indicating an | |
1052 | uncaught exception, and then exits. In practice, it is quite difficult | |
1053 | to observe this behaviour, because Guile when used interactively | |
1054 | installs a top level @code{catch} handler that will catch all exceptions | |
1055 | and print an appropriate error message @emph{without} exiting. For | |
1056 | example, this is what happens if you try to throw an unhandled exception | |
1057 | in the standard Guile REPL; note that Guile's command loop continues | |
1058 | after the error message: | |
1059 | ||
1060 | @lisp | |
1061 | guile> (throw 'badex) | |
1062 | <unnamed port>:3:1: In procedure gsubr-apply @dots{} | |
1063 | <unnamed port>:3:1: unhandled-exception: badex | |
1064 | ABORT: (misc-error) | |
1065 | guile> | |
1066 | @end lisp | |
1067 | ||
1068 | The default uncaught exception behaviour can be observed by evaluating a | |
1069 | @code{throw} expression from the shell command line: | |
1070 | ||
1071 | @example | |
1072 | $ guile -c "(begin (throw 'badex) (display \"here\\n\"))" | |
1073 | guile: uncaught throw to badex: () | |
1074 | $ | |
1075 | @end example | |
1076 | ||
1077 | @noindent | |
1078 | That Guile exits immediately following the uncaught exception | |
1079 | is shown by the absence of any output from the @code{display} | |
1080 | expression, because Guile never gets to the point of evaluating that | |
1081 | expression. | |
1082 | ||
07d83abe MV |
1083 | |
1084 | @node Exception Implementation | |
1085 | @subsubsection How Guile Implements Exceptions | |
1086 | ||
1087 | It is traditional in Scheme to implement exception systems using | |
1088 | @code{call-with-current-continuation}. Continuations | |
1089 | (@pxref{Continuations}) are such a powerful concept that any other | |
1090 | control mechanism --- including @code{catch} and @code{throw} --- can be | |
1091 | implemented in terms of them. | |
1092 | ||
1093 | Guile does not implement @code{catch} and @code{throw} like this, | |
1094 | though. Why not? Because Guile is specifically designed to be easy to | |
1095 | integrate with applications written in C. In a mixed Scheme/C | |
1096 | environment, the concept of @dfn{continuation} must logically include | |
1097 | ``what happens next'' in the C parts of the application as well as the | |
1098 | Scheme parts, and it turns out that the only reasonable way of | |
1099 | implementing continuations like this is to save and restore the complete | |
1100 | C stack. | |
1101 | ||
1102 | So Guile's implementation of @code{call-with-current-continuation} is a | |
1103 | stack copying one. This allows it to interact well with ordinary C | |
1104 | code, but means that creating and calling a continuation is slowed down | |
1105 | by the time that it takes to copy the C stack. | |
1106 | ||
1107 | The more targeted mechanism provided by @code{catch} and @code{throw} | |
1108 | does not need to save and restore the C stack because the @code{throw} | |
1109 | always jumps to a location higher up the stack of the code that executes | |
1110 | the @code{throw}. Therefore Guile implements the @code{catch} and | |
1111 | @code{throw} primitives independently of | |
1112 | @code{call-with-current-continuation}, in a way that takes advantage of | |
1113 | this @emph{upwards only} nature of exceptions. | |
1114 | ||
1115 | ||
1116 | @node Error Reporting | |
1117 | @subsection Procedures for Signaling Errors | |
1118 | ||
1119 | Guile provides a set of convenience procedures for signaling error | |
1120 | conditions that are implemented on top of the exception primitives just | |
1121 | described. | |
1122 | ||
1123 | @deffn {Scheme Procedure} error msg args @dots{} | |
1124 | Raise an error with key @code{misc-error} and a message constructed by | |
1125 | displaying @var{msg} and writing @var{args}. | |
1126 | @end deffn | |
1127 | ||
1128 | @deffn {Scheme Procedure} scm-error key subr message args data | |
1129 | @deffnx {C Function} scm_error_scm (key, subr, message, args, data) | |
1130 | Raise an error with key @var{key}. @var{subr} can be a string | |
1131 | naming the procedure associated with the error, or @code{#f}. | |
1132 | @var{message} is the error message string, possibly containing | |
1133 | @code{~S} and @code{~A} escapes. When an error is reported, | |
1134 | these are replaced by formatting the corresponding members of | |
1135 | @var{args}: @code{~A} (was @code{%s} in older versions of | |
1136 | Guile) formats using @code{display} and @code{~S} (was | |
1137 | @code{%S}) formats using @code{write}. @var{data} is a list or | |
1138 | @code{#f} depending on @var{key}: if @var{key} is | |
1139 | @code{system-error} then it should be a list containing the | |
1140 | Unix @code{errno} value; If @var{key} is @code{signal} then it | |
7cd44c6d MV |
1141 | should be a list containing the Unix signal number; If |
1142 | @var{key} is @code{out-of-range} or @code{wrong-type-arg}, | |
1143 | it is a list containing the bad value; otherwise | |
07d83abe MV |
1144 | it will usually be @code{#f}. |
1145 | @end deffn | |
1146 | ||
1147 | @deffn {Scheme Procedure} strerror err | |
1148 | @deffnx {C Function} scm_strerror (err) | |
44ba562e KR |
1149 | Return the Unix error message corresponding to @var{err}, an integer |
1150 | @code{errno} value. | |
1151 | ||
1152 | When @code{setlocale} has been called (@pxref{Locales}), the message | |
1153 | is in the language and charset of @code{LC_MESSAGES}. (This is done | |
1154 | by the C library.) | |
07d83abe MV |
1155 | @end deffn |
1156 | ||
1157 | @c begin (scm-doc-string "boot-9.scm" "false-if-exception") | |
1158 | @deffn syntax false-if-exception expr | |
1159 | Returns the result of evaluating its argument; however | |
1160 | if an exception occurs then @code{#f} is returned instead. | |
1161 | @end deffn | |
1162 | @c end | |
1163 | ||
1164 | ||
1165 | @node Dynamic Wind | |
1166 | @subsection Dynamic Wind | |
1167 | ||
661ae7ab MV |
1168 | For Scheme code, the fundamental procedure to react to non-local entry |
1169 | and exits of dynamic contexts is @code{dynamic-wind}. C code could | |
1170 | use @code{scm_internal_dynamic_wind}, but since C does not allow the | |
1171 | convenient construction of anonymous procedures that close over | |
1172 | lexical variables, this will be, well, inconvenient. | |
1173 | ||
1174 | Therefore, Guile offers the functions @code{scm_dynwind_begin} and | |
1175 | @code{scm_dynwind_end} to delimit a dynamic extent. Within this | |
a1ef7406 | 1176 | dynamic extent, which is called a @dfn{dynwind context}, you can |
661ae7ab MV |
1177 | perform various @dfn{dynwind actions} that control what happens when |
1178 | the dynwind context is entered or left. For example, you can register | |
1179 | a cleanup routine with @code{scm_dynwind_unwind_handler} that is | |
1180 | executed when the context is left. There are several other more | |
1181 | specialized dynwind actions as well, for example to temporarily block | |
1182 | the execution of asyncs or to temporarily change the current output | |
1183 | port. They are described elsewhere in this manual. | |
1184 | ||
1185 | Here is an example that shows how to prevent memory leaks. | |
1186 | ||
1187 | @example | |
1188 | ||
1189 | /* Suppose there is a function called FOO in some library that you | |
1190 | would like to make available to Scheme code (or to C code that | |
1191 | follows the Scheme conventions). | |
1192 | ||
1193 | FOO takes two C strings and returns a new string. When an error has | |
1194 | occurred in FOO, it returns NULL. | |
1195 | */ | |
1196 | ||
1197 | char *foo (char *s1, char *s2); | |
1198 | ||
1199 | /* SCM_FOO interfaces the C function FOO to the Scheme way of life. | |
1200 | It takes care to free up all temporary strings in the case of | |
1201 | non-local exits. | |
1202 | */ | |
1203 | ||
1204 | SCM | |
1205 | scm_foo (SCM s1, SCM s2) | |
1206 | @{ | |
1207 | char *c_s1, *c_s2, *c_res; | |
1208 | ||
1209 | scm_dynwind_begin (0); | |
1210 | ||
1211 | c_s1 = scm_to_locale_string (s1); | |
1212 | ||
1213 | /* Call 'free (c_s1)' when the dynwind context is left. | |
1214 | */ | |
1215 | scm_dynwind_unwind_handler (free, c_s1, SCM_F_WIND_EXPLICITLY); | |
1216 | ||
1217 | c_s2 = scm_to_locale_string (s2); | |
1218 | ||
1219 | /* Same as above, but more concisely. | |
1220 | */ | |
1221 | scm_dynwind_free (c_s2); | |
1222 | ||
1223 | c_res = foo (c_s1, c_s2); | |
1224 | if (c_res == NULL) | |
1225 | scm_memory_error ("foo"); | |
1226 | ||
1227 | scm_dynwind_end (); | |
1228 | ||
1229 | return scm_take_locale_string (res); | |
1230 | @} | |
1231 | @end example | |
1232 | ||
07d83abe MV |
1233 | @rnindex dynamic-wind |
1234 | @deffn {Scheme Procedure} dynamic-wind in_guard thunk out_guard | |
1235 | @deffnx {C Function} scm_dynamic_wind (in_guard, thunk, out_guard) | |
1236 | All three arguments must be 0-argument procedures. | |
1237 | @var{in_guard} is called, then @var{thunk}, then | |
1238 | @var{out_guard}. | |
1239 | ||
1240 | If, any time during the execution of @var{thunk}, the | |
1241 | dynamic extent of the @code{dynamic-wind} expression is escaped | |
1242 | non-locally, @var{out_guard} is called. If the dynamic extent of | |
1243 | the dynamic-wind is re-entered, @var{in_guard} is called. Thus | |
1244 | @var{in_guard} and @var{out_guard} may be called any number of | |
1245 | times. | |
40296bab | 1246 | |
07d83abe MV |
1247 | @lisp |
1248 | (define x 'normal-binding) | |
1249 | @result{} x | |
40296bab KR |
1250 | (define a-cont |
1251 | (call-with-current-continuation | |
1252 | (lambda (escape) | |
1253 | (let ((old-x x)) | |
1254 | (dynamic-wind | |
1255 | ;; in-guard: | |
1256 | ;; | |
1257 | (lambda () (set! x 'special-binding)) | |
1258 | ||
1259 | ;; thunk | |
1260 | ;; | |
1261 | (lambda () (display x) (newline) | |
1262 | (call-with-current-continuation escape) | |
1263 | (display x) (newline) | |
1264 | x) | |
1265 | ||
1266 | ;; out-guard: | |
1267 | ;; | |
1268 | (lambda () (set! x old-x))))))) | |
07d83abe MV |
1269 | ;; Prints: |
1270 | special-binding | |
1271 | ;; Evaluates to: | |
1272 | @result{} a-cont | |
1273 | x | |
1274 | @result{} normal-binding | |
1275 | (a-cont #f) | |
1276 | ;; Prints: | |
1277 | special-binding | |
1278 | ;; Evaluates to: | |
1279 | @result{} a-cont ;; the value of the (define a-cont...) | |
1280 | x | |
1281 | @result{} normal-binding | |
1282 | a-cont | |
1283 | @result{} special-binding | |
1284 | @end lisp | |
1285 | @end deffn | |
1286 | ||
98241dc5 NJ |
1287 | @deftp {C Type} scm_t_dynwind_flags |
1288 | This is an enumeration of several flags that modify the behavior of | |
1289 | @code{scm_dynwind_begin}. The flags are listed in the following | |
1290 | table. | |
1291 | ||
1292 | @table @code | |
1293 | @item SCM_F_DYNWIND_REWINDABLE | |
1294 | The dynamic context is @dfn{rewindable}. This means that it can be | |
72b3aa56 | 1295 | reentered non-locally (via the invocation of a continuation). The |
98241dc5 NJ |
1296 | default is that a dynwind context can not be reentered non-locally. |
1297 | @end table | |
1298 | ||
1299 | @end deftp | |
1300 | ||
1301 | @deftypefn {C Function} void scm_dynwind_begin (scm_t_dynwind_flags flags) | |
661ae7ab MV |
1302 | The function @code{scm_dynwind_begin} starts a new dynamic context and |
1303 | makes it the `current' one. | |
07d83abe | 1304 | |
661ae7ab MV |
1305 | The @var{flags} argument determines the default behavior of the |
1306 | context. Normally, use 0. This will result in a context that can not | |
1307 | be reentered with a captured continuation. When you are prepared to | |
1308 | handle reentries, include @code{SCM_F_DYNWIND_REWINDABLE} in | |
1309 | @var{flags}. | |
07d83abe MV |
1310 | |
1311 | Being prepared for reentry means that the effects of unwind handlers | |
1312 | can be undone on reentry. In the example above, we want to prevent a | |
1313 | memory leak on non-local exit and thus register an unwind handler that | |
1314 | frees the memory. But once the memory is freed, we can not get it | |
1315 | back on reentry. Thus reentry can not be allowed. | |
1316 | ||
1317 | The consequence is that continuations become less useful when | |
ecb87335 | 1318 | non-reentrant contexts are captured, but you don't need to worry |
661ae7ab MV |
1319 | about that too much. |
1320 | ||
1321 | The context is ended either implicitly when a non-local exit happens, | |
1322 | or explicitly with @code{scm_dynwind_end}. You must make sure that a | |
1323 | dynwind context is indeed ended properly. If you fail to call | |
1324 | @code{scm_dynwind_end} for each @code{scm_dynwind_begin}, the behavior | |
1325 | is undefined. | |
07d83abe MV |
1326 | @end deftypefn |
1327 | ||
661ae7ab MV |
1328 | @deftypefn {C Function} void scm_dynwind_end () |
1329 | End the current dynamic context explicitly and make the previous one | |
1330 | current. | |
07d83abe MV |
1331 | @end deftypefn |
1332 | ||
98241dc5 NJ |
1333 | @deftp {C Type} scm_t_wind_flags |
1334 | This is an enumeration of several flags that modify the behavior of | |
1335 | @code{scm_dynwind_unwind_handler} and | |
1336 | @code{scm_dynwind_rewind_handler}. The flags are listed in the | |
1337 | following table. | |
1338 | ||
1339 | @table @code | |
1340 | @item SCM_F_WIND_EXPLICITLY | |
1341 | @vindex SCM_F_WIND_EXPLICITLY | |
1342 | The registered action is also carried out when the dynwind context is | |
1343 | entered or left locally. | |
1344 | @end table | |
1345 | @end deftp | |
1346 | ||
1347 | @deftypefn {C Function} void scm_dynwind_unwind_handler (void (*func)(void *), void *data, scm_t_wind_flags flags) | |
1348 | @deftypefnx {C Function} void scm_dynwind_unwind_handler_with_scm (void (*func)(SCM), SCM data, scm_t_wind_flags flags) | |
07d83abe | 1349 | Arranges for @var{func} to be called with @var{data} as its arguments |
661ae7ab MV |
1350 | when the current context ends implicitly. If @var{flags} contains |
1351 | @code{SCM_F_WIND_EXPLICITLY}, @var{func} is also called when the | |
1352 | context ends explicitly with @code{scm_dynwind_end}. | |
07d83abe | 1353 | |
661ae7ab | 1354 | The function @code{scm_dynwind_unwind_handler_with_scm} takes care that |
07d83abe MV |
1355 | @var{data} is protected from garbage collection. |
1356 | @end deftypefn | |
1357 | ||
98241dc5 NJ |
1358 | @deftypefn {C Function} void scm_dynwind_rewind_handler (void (*func)(void *), void *data, scm_t_wind_flags flags) |
1359 | @deftypefnx {C Function} void scm_dynwind_rewind_handler_with_scm (void (*func)(SCM), SCM data, scm_t_wind_flags flags) | |
07d83abe | 1360 | Arrange for @var{func} to be called with @var{data} as its argument when |
661ae7ab | 1361 | the current context is restarted by rewinding the stack. When @var{flags} |
07d83abe MV |
1362 | contains @code{SCM_F_WIND_EXPLICITLY}, @var{func} is called immediately |
1363 | as well. | |
1364 | ||
661ae7ab | 1365 | The function @code{scm_dynwind_rewind_handler_with_scm} takes care that |
07d83abe MV |
1366 | @var{data} is protected from garbage collection. |
1367 | @end deftypefn | |
1368 | ||
9f1ba6a9 NJ |
1369 | @deftypefn {C Function} void scm_dynwind_free (void *mem) |
1370 | Arrange for @var{mem} to be freed automatically whenever the current | |
1371 | context is exited, whether normally or non-locally. | |
1372 | @code{scm_dynwind_free (mem)} is an equivalent shorthand for | |
1373 | @code{scm_dynwind_unwind_handler (free, mem, SCM_F_WIND_EXPLICITLY)}. | |
1374 | @end deftypefn | |
1375 | ||
07d83abe MV |
1376 | |
1377 | @node Handling Errors | |
1378 | @subsection How to Handle Errors | |
1379 | ||
1380 | Error handling is based on @code{catch} and @code{throw}. Errors are | |
1381 | always thrown with a @var{key} and four arguments: | |
1382 | ||
1383 | @itemize @bullet | |
1384 | @item | |
1385 | @var{key}: a symbol which indicates the type of error. The symbols used | |
1386 | by libguile are listed below. | |
1387 | ||
1388 | @item | |
1389 | @var{subr}: the name of the procedure from which the error is thrown, or | |
1390 | @code{#f}. | |
1391 | ||
1392 | @item | |
1393 | @var{message}: a string (possibly language and system dependent) | |
1394 | describing the error. The tokens @code{~A} and @code{~S} can be | |
1395 | embedded within the message: they will be replaced with members of the | |
1396 | @var{args} list when the message is printed. @code{~A} indicates an | |
1397 | argument printed using @code{display}, while @code{~S} indicates an | |
1398 | argument printed using @code{write}. @var{message} can also be | |
1399 | @code{#f}, to allow it to be derived from the @var{key} by the error | |
1400 | handler (may be useful if the @var{key} is to be thrown from both C and | |
1401 | Scheme). | |
1402 | ||
1403 | @item | |
1404 | @var{args}: a list of arguments to be used to expand @code{~A} and | |
1405 | @code{~S} tokens in @var{message}. Can also be @code{#f} if no | |
1406 | arguments are required. | |
1407 | ||
1408 | @item | |
1409 | @var{rest}: a list of any additional objects required. e.g., when the | |
1410 | key is @code{'system-error}, this contains the C errno value. Can also | |
1411 | be @code{#f} if no additional objects are required. | |
1412 | @end itemize | |
1413 | ||
1414 | In addition to @code{catch} and @code{throw}, the following Scheme | |
1415 | facilities are available: | |
1416 | ||
7545ddd4 AW |
1417 | @deffn {Scheme Procedure} display-error frame port subr message args rest |
1418 | @deffnx {C Function} scm_display_error (frame, port, subr, message, args, rest) | |
07d83abe | 1419 | Display an error message to the output port @var{port}. |
7545ddd4 | 1420 | @var{frame} is the frame in which the error occurred, @var{subr} is |
07d83abe MV |
1421 | the name of the procedure in which the error occurred and |
1422 | @var{message} is the actual error message, which may contain | |
1423 | formatting instructions. These will format the arguments in | |
1424 | the list @var{args} accordingly. @var{rest} is currently | |
1425 | ignored. | |
1426 | @end deffn | |
1427 | ||
1428 | The following are the error keys defined by libguile and the situations | |
1429 | in which they are used: | |
1430 | ||
1431 | @itemize @bullet | |
1432 | @item | |
1433 | @cindex @code{error-signal} | |
1434 | @code{error-signal}: thrown after receiving an unhandled fatal signal | |
1435 | such as SIGSEGV, SIGBUS, SIGFPE etc. The @var{rest} argument in the throw | |
1436 | contains the coded signal number (at present this is not the same as the | |
1437 | usual Unix signal number). | |
1438 | ||
1439 | @item | |
1440 | @cindex @code{system-error} | |
1441 | @code{system-error}: thrown after the operating system indicates an | |
1442 | error condition. The @var{rest} argument in the throw contains the | |
1443 | errno value. | |
1444 | ||
1445 | @item | |
1446 | @cindex @code{numerical-overflow} | |
1447 | @code{numerical-overflow}: numerical overflow. | |
1448 | ||
1449 | @item | |
1450 | @cindex @code{out-of-range} | |
1451 | @code{out-of-range}: the arguments to a procedure do not fall within the | |
1452 | accepted domain. | |
1453 | ||
1454 | @item | |
1455 | @cindex @code{wrong-type-arg} | |
1456 | @code{wrong-type-arg}: an argument to a procedure has the wrong type. | |
1457 | ||
1458 | @item | |
1459 | @cindex @code{wrong-number-of-args} | |
1460 | @code{wrong-number-of-args}: a procedure was called with the wrong number | |
1461 | of arguments. | |
1462 | ||
1463 | @item | |
1464 | @cindex @code{memory-allocation-error} | |
1465 | @code{memory-allocation-error}: memory allocation error. | |
1466 | ||
1467 | @item | |
1468 | @cindex @code{stack-overflow} | |
1469 | @code{stack-overflow}: stack overflow error. | |
1470 | ||
1471 | @item | |
1472 | @cindex @code{regular-expression-syntax} | |
1473 | @code{regular-expression-syntax}: errors generated by the regular | |
1474 | expression library. | |
1475 | ||
1476 | @item | |
1477 | @cindex @code{misc-error} | |
1478 | @code{misc-error}: other errors. | |
1479 | @end itemize | |
1480 | ||
1481 | ||
1482 | @subsubsection C Support | |
1483 | ||
1484 | In the following C functions, @var{SUBR} and @var{MESSAGE} parameters | |
1485 | can be @code{NULL} to give the effect of @code{#f} described above. | |
1486 | ||
1487 | @deftypefn {C Function} SCM scm_error (SCM @var{key}, char *@var{subr}, char *@var{message}, SCM @var{args}, SCM @var{rest}) | |
9a18d8d4 | 1488 | Throw an error, as per @code{scm-error} (@pxref{Error Reporting}). |
07d83abe MV |
1489 | @end deftypefn |
1490 | ||
1491 | @deftypefn {C Function} void scm_syserror (char *@var{subr}) | |
1492 | @deftypefnx {C Function} void scm_syserror_msg (char *@var{subr}, char *@var{message}, SCM @var{args}) | |
1493 | Throw an error with key @code{system-error} and supply @code{errno} in | |
1494 | the @var{rest} argument. For @code{scm_syserror} the message is | |
1495 | generated using @code{strerror}. | |
1496 | ||
1497 | Care should be taken that any code in between the failing operation | |
1498 | and the call to these routines doesn't change @code{errno}. | |
1499 | @end deftypefn | |
1500 | ||
1501 | @deftypefn {C Function} void scm_num_overflow (char *@var{subr}) | |
1502 | @deftypefnx {C Function} void scm_out_of_range (char *@var{subr}, SCM @var{bad_value}) | |
1503 | @deftypefnx {C Function} void scm_wrong_num_args (SCM @var{proc}) | |
1504 | @deftypefnx {C Function} void scm_wrong_type_arg (char *@var{subr}, int @var{argnum}, SCM @var{bad_value}) | |
58228cc6 | 1505 | @deftypefnx {C Function} void scm_wrong_type_arg_msg (char *@var{subr}, int @var{argnum}, SCM @var{bad_value}, const char *@var{expected}) |
07d83abe MV |
1506 | @deftypefnx {C Function} void scm_memory_error (char *@var{subr}) |
1507 | Throw an error with the various keys described above. | |
9dfcd9e2 | 1508 | @deftypefnx {C Function} void scm_misc_error (const char *@var{subr}, const char *@var{message}, SCM @var{args}) |
07d83abe | 1509 | |
9dfcd9e2 | 1510 | In @code{scm_wrong_num_args}, @var{proc} should be a Scheme symbol |
58228cc6 NJ |
1511 | which is the name of the procedure incorrectly invoked. The other |
1512 | routines take the name of the invoked procedure as a C string. | |
1513 | ||
1514 | In @code{scm_wrong_type_arg_msg}, @var{expected} is a C string | |
1515 | describing the type of argument that was expected. | |
9dfcd9e2 LC |
1516 | |
1517 | In @code{scm_misc_error}, @var{message} is the error message string, | |
1518 | possibly containing @code{simple-format} escapes (@pxref{Writing}), and | |
1519 | the corresponding arguments in the @var{args} list. | |
07d83abe MV |
1520 | @end deftypefn |
1521 | ||
1522 | ||
0f7e6c56 AW |
1523 | @subsubsection Signalling Type Errors |
1524 | ||
1525 | Every function visible at the Scheme level should aggressively check the | |
1526 | types of its arguments, to avoid misinterpreting a value, and perhaps | |
1527 | causing a segmentation fault. Guile provides some macros to make this | |
1528 | easier. | |
1529 | ||
1530 | @deftypefn Macro void SCM_ASSERT (int @var{test}, SCM @var{obj}, unsigned int @var{position}, const char *@var{subr}) | |
1531 | If @var{test} is zero, signal a ``wrong type argument'' error, | |
1532 | attributed to the subroutine named @var{subr}, operating on the value | |
1533 | @var{obj}, which is the @var{position}'th argument of @var{subr}. | |
1534 | @end deftypefn | |
1535 | ||
1536 | @deftypefn Macro int SCM_ARG1 | |
1537 | @deftypefnx Macro int SCM_ARG2 | |
1538 | @deftypefnx Macro int SCM_ARG3 | |
1539 | @deftypefnx Macro int SCM_ARG4 | |
1540 | @deftypefnx Macro int SCM_ARG5 | |
1541 | @deftypefnx Macro int SCM_ARG6 | |
1542 | @deftypefnx Macro int SCM_ARG7 | |
1543 | One of the above values can be used for @var{position} to indicate the | |
1544 | number of the argument of @var{subr} which is being checked. | |
1545 | Alternatively, a positive integer number can be used, which allows to | |
1546 | check arguments after the seventh. However, for parameter numbers up to | |
1547 | seven it is preferable to use @code{SCM_ARGN} instead of the | |
1548 | corresponding raw number, since it will make the code easier to | |
1549 | understand. | |
1550 | @end deftypefn | |
1551 | ||
1552 | @deftypefn Macro int SCM_ARGn | |
1553 | Passing a value of zero or @code{SCM_ARGn} for @var{position} allows to | |
1554 | leave it unspecified which argument's type is incorrect. Again, | |
1555 | @code{SCM_ARGn} should be preferred over a raw zero constant. | |
1556 | @end deftypefn | |
1557 | ||
1558 | ||
ce2612cd NJ |
1559 | @node Continuation Barriers |
1560 | @subsection Continuation Barriers | |
1561 | ||
1562 | The non-local flow of control caused by continuations might sometimes | |
56664c08 AW |
1563 | not be wanted. You can use @code{with-continuation-barrier} to erect |
1564 | fences that continuations can not pass. | |
ce2612cd NJ |
1565 | |
1566 | @deffn {Scheme Procedure} with-continuation-barrier proc | |
1567 | @deffnx {C Function} scm_with_continuation_barrier (proc) | |
1568 | Call @var{proc} and return its result. Do not allow the invocation of | |
1569 | continuations that would leave or enter the dynamic extent of the call | |
1570 | to @code{with-continuation-barrier}. Such an attempt causes an error | |
1571 | to be signaled. | |
1572 | ||
1573 | Throws (such as errors) that are not caught from within @var{proc} are | |
1574 | caught by @code{with-continuation-barrier}. In that case, a short | |
1575 | message is printed to the current error port and @code{#f} is returned. | |
1576 | ||
1577 | Thus, @code{with-continuation-barrier} returns exactly once. | |
1578 | @end deffn | |
1579 | ||
1580 | @deftypefn {C Function} {void *} scm_c_with_continuation_barrier (void *(*func) (void *), void *data) | |
1581 | Like @code{scm_with_continuation_barrier} but call @var{func} on | |
1582 | @var{data}. When an error is caught, @code{NULL} is returned. | |
1583 | @end deftypefn | |
1584 | ||
1585 | ||
07d83abe MV |
1586 | @c Local Variables: |
1587 | @c TeX-master: "guile.texi" | |
1588 | @c End: |