[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: let-keywords?

From: Kevin Ryde
Subject: Re: let-keywords?
Date: Thu, 14 Dec 2006 08:28:37 +1100
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux)

I checked in the text below. let-keywords Reference

`let-keywords' and `let-keywords*' extract values from keyword style
argument lists, binding local variables to those values or to defaults.

 -- library syntax: let-keywords args allow-other-keys? (binding ...)
          body ...
 -- library syntax: let-keywords* args allow-other-keys? (binding ...)
          body ...
     ARGS is evaluated and should give a list of the form `(#:keyword1
     value1 #:keyword2 value2 ...)'.  The BINDINGs are variables and
     default expressions, with the variables to be set (by name) from
     the keyword values.  The BODY forms are then evaluated and the
     last is the result.  An example will make the syntax clearest,

          (define args '(#:xyzzy "hello" #:foo "world"))

          (let-keywords args #t
                ((foo  "default for foo")
                 (bar  (string-append "default" "for" "bar")))
            (display foo)
            (display ", ")
            (display bar))
          -| world, defaultforbar

     The binding for `foo' comes from the `#:foo' keyword in `args'.
     But the binding for `bar' is the default in the `let-keywords',
     since there's no `#:bar' in the args.

     ALLOW-OTHER-KEYS? is evaluated and controls whether unknown
     keywords are allowed in the ARGS list.  When true other keys are
     ignored (such as `#:xyzzy' in the example), when `#f' an error is
     thrown for anything unknown.

     `let-keywords' is like `let' (*note Local Bindings::) in that all
     bindings are made at once, the defaults expressions are evaluated
     (if needed) outside the scope of the `let-keywords'.

     `let-keywords*' is like `let*', each binding is made successively,
     and the default expressions see the bindings previously made.
     This is the style used by `lambda*' keywords (*note lambda*
     Reference::).  For example,

          (define args '(#:foo 3))

          (let-keywords* args #f
                ((foo  99)
                 (bar  (+ foo 6)))
            (display bar))
          -| 9

     The expression for each default is only evaluated if it's needed,
     ie. if the keyword doesn't appear in ARGS.  So one way to make a
     keyword mandatory is to throw an error of some sort as the default.

          (define args '(#:start 7 #:finish 13))

          (let-keywords* args #t
                ((start 0)
                 (stop  (error "missing #:stop argument")))
            (display bar))
          => ERROR: missing #:stop argument

reply via email to

[Prev in Thread] Current Thread [Next in Thread]