Next: , Previous: , Up: iklib syntaxes   [Index]


6.8.8 Early return from code blocks

The standardised Scheme languages do not define the common return keyword present in many languages, like C and Python. Despite this, it is quite easy to obtain the “early return” functionality through the use of continuations:

(call/cc
    (lambda (return)
      (display 'before)
      (return 1)
      (display 'never)))
-| before
⇒ 1

Many programmers are used to the return keyword, so Vicare includes some syntaxes to support it. The use of all these syntaxes involves the creation of a continuation, which is a performance penalty.

Fluid Syntax: return ?expr ...

This syntax is meant to be used to return from some enclosing block, returning the given optional arguments. Being a fluid syntax: it is possible to rebind this keyword in custom syntaxes.

Syntax: returnable ?body0 ?body

Like begin, but allow the use of the keyword return to return values to the enclosing continuation.

(returnable
  (display 'before)
  (return)
  (display 'never))
-| before

(returnable
  (display 'before)
  (return 1)
  (display 'never))
-| before
⇒ 1

(returnable
  (display 'before)
  (return 1 2)
  (display 'never))
-| before
⇒ 1 2