[plt-scheme] keyword arguments (in v4.0, maybe)

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Jun 12 02:55:27 EDT 2007

On Jun 12, Matthew Flatt wrote:
> 
> [...]

Two quick notes:

> Another drawback is that not all styles of keywords supported by
> "kw.ss" would be the allowed in the new form.

This one should not be a big problem, since Matthew's design tries to
make keyword arguments saner at the expense of some uses that are not
too popular (or at least it seems that way).  For example, the kw
library has `#:flag's which are like keywords that have no value
(the arguments default to #f, and just metioning the keyword makes the
argument #t), and it also has #:body which allows chaining positional
and keyword arguments as in unix command-line syntax.  These featues
will go away.

Another thing that becomes less convenient is using keywords as plain
values.  When you do that, you'll need to quote the keyword.  For
example,

  (define (run proc mode)
    (if (eq? mode #:fast) ...))

becomes

  (define (run proc mode)
    (if (eq? mode '#:fast) ...))

The "'#:" prefix is very heavy -- but (IMO) this is justified by the
fact that if you use it then you should consider using symbols
instead.


> One drawback of this strategy is that `(lambda args (apply f args))'
> is not an all-purpose wrapper strategy, because that only works for
> `f' that doesn't take keyword arguments. But there would be a new
> way to create wrappers that works for both plain and
> keyword-accepting procedures.

Note that this one is a radical change.  It means that using keywords
will "infect" code: using keyword-unaware code will not be possible
with keyword aware code.  For example, SRFI code (or any portable
Scheme code) can assume that

  (lambda args (apply f args))

is exactly equivalent to `f' -- but this will not be the case for
keyword procedures.  PLT code that currently uses this property
(`trace', for example) will be updated, and I assume that planet code
will too (given time, since this is a backward-incompatible change),
but portable higher-order code will generally not work with
keyword-based procedures.


> While we're at it, I think the form for declaring keyword-based
> procedures more parallel to the application side --- more like SRFI-89
> than "kw.ss".

To clarify this -- something like (imaginery syntax):

  (define (foo a b [c "Zee"] [d #:x] [e #:y "Bee"]) ...)

will define `foo' as a procedure with two required arguments, one
optional argument, a mandatory `#:x' keyword argument (bound to `d')
(error if it is not supplied in an application), and an optional `#:y'
keyword.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.