For Development HEAD DRAFTSearch (procedure/syntax/module):

11.59 srfi.227 - Optional arguments

Module: srfi.227
Module: srfi.227.definitions

This srfi provies macros to specify optional arguments portably. Gauche supports optional arguments in extended formals (see Making procedures), but it is not portable.

The main module, srfi.227, exports macros opt-lambda, opt*-lambda, let-optionals, and let-optionals*. Note that this let-optionals* has different semantics from Gauche’s built-in let-optionals*.

The submodule srfi.227.definitions exports two more macros, define-optionals and define-optionals*.

Macro: opt-labmda opt-formals body …
Macro: opt*-lambda opt-formals body …

[SRFI-227]{srfi.227} Evaliates to a procedure, similar to lambda.

The opt-formals is either one of the following form:

(var ... (optvar init) ...)
(var ... (optvar init) ... . restvar)

Var, optvar, and restvar are identifiers. Each init is an expression.

Var … are required arguments. Optvar … are optional arguments, and when no corresponding parameter is provided, init is evaluated and used as the value of the argument.

In the first form, giving exessive arguments throws an error; in the second form, excessive arguments are bound to restvar as a list.

The scope of init differs between opt-lambda and opt-lambda*. With opt-lambda, inits are evaluated in the scope where opt-lambda is placed. With opt*-lambda, the scope of inits also includes preceding vars and optvars.

(let ((x 1))
  ((opt-lambda (x (y (+ x 1))) (list x y)) 10))
 ⇒ (10 2)

(let ((x 1))
  ((opt*-lambda (x (y (+ x 1))) (list x y)) 10))
 ⇒ (10 11)

Note: (opt*-lambda (v … (w init) …) body … is the same as Gauche’s (lambda (v … :optional (w init) …) body …).

Macro: let-optionals expr opt-formals body …
Macro: let-optionals* expr opt-formals body …

[SRFI-227]{srfi.227} Syntax sugar to decompose the result of expr using opt-formals:

(let-optionals expr opt-formals body ...)
 ≡ ((opt-lambda opt-formals body ...) expr)

(let-optionals* expr opt-formals body ...)
 ≡ ((opt*-lambda opt-formals body ...) expr)

Note: Gauche has built-in let-optionals*, which is different from this srfi. Gauche’s doesn’t allow requried parameters. See Making procedures, for the details.

Macro: define-optionals (name . opt-formals) body …
Macro: define-optionals* (name . opt-formals) body …

[SRFI-227]{srfi.227.definitions} These two forms are provided in a submodule srfi.227.definitions.

(define-optionals (name . opt-formals) body ...)
 ≡ (define name (opt-lambda opt-formals body ...))

(define-optionals* (name . opt-formals) body ...)
 ≡ (define name (opt*-lambda opt-formals body ...))


For Development HEAD DRAFTSearch (procedure/syntax/module):
DRAFT