Guile Library

(scheme kwargs)

Overview

Support for defining functions that take python-like keyword arguments. In one of his early talks, Paul Graham wrote about a large system called "Rtml":

Most of the operators in Rtml were designed to take keyword parameters, and what a help that turned out to be. If I wanted to add another dimension to the behavior of one of the operators, I could just add a new keyword parameter, and everyone's existing templates would continue to work. A few of the Rtml operators didn't take keyword parameters, because I didn't think I'd ever need to change them, and almost every one I ended up kicking myself about later. If I could go back and start over from scratch, one of the things I'd change would be that I'd make every Rtml operator take keyword parameters.

See lambda/kwargs, for documentation and examples.

See Optional Arguments, for more information on Guile's standard support for optional and keyword arguments. Quote taken from http://lib.store.yahoo.net/lib/paulgraham/bbnexcerpts.txt.

Usage

define/kwargs args
[Special Form]

Defines a function that takes kwargs. See scheme kwargs lambda/kwargs, for more information.

lambda/kwargs args
[Special Form]

Defines a function that takes keyword arguments.

bindings is a list of bindings, each of which may either be a symbol or a two-element symbol-and-default-value list. Symbols without specified default values will default to #f.

For example:

 (define frobulate (lambda/kwargs (foo (bar 13) (baz 42))
                     (list foo bar baz)))
 (frobulate) ⇒ (#f 13 42)
 (frobulate #:baz 3) ⇒ (#f 13 3)
 (frobulate #:foo 3) ⇒ (3 13 42)
 (frobulate 3 4) ⇒ (3 4 42)
 (frobulate 1 2 3) ⇒ (1 2 3)
 (frobulate #:baz 2 #:bar 1) ⇒ (#f 1 2)
 (frobulate 10 20 #:foo 3) ⇒ (3 20 42)

This function differs from the standard lambda* provided by Guile in that invoking the function will accept positional arguments. As an example, the lambda/kwargs behaves more intuitively in the following case:

 ((lambda* (#:optional (bar 42) #:key (baz 73))
    (list bar baz))
  1 2) ⇒ (1 73)
 ((lambda/kwargs ((bar 42) (baz 73))
    (list bar baz))
  1 2) ⇒ (1 2)

The fact that lambda* accepts the extra 2 argument is probably just a bug. In any case, lambda/kwargs does the right thing.