[plt-scheme] passing-on keyword arguments

From: Dimitris Vyzovitis (vyzo at media.mit.edu)
Date: Tue Mar 18 20:33:19 EDT 2008

On Tue, 18 Mar 2008, Eli Barzilay wrote:

> On Mar 18, Dimitris Vyzovitis wrote:
> > On Tue, 18 Mar 2008, Eli Barzilay wrote:
> > >
> > > I think that I know how to make it easier, allowing something like
> > > this:
> > >
> > >   (define (outer args ...)
> > >     (+ (inner args ...) 5))
> > >
> > > to do the same, but it'll take some work to implement.
> >
> > If you are about to embark on 'fixing' the keywords (as in make them
> > easier to compose), can we get a form that captures straight keywords and
> > an easier to use wrapper for keyword-apply?
> > I was thinking something like this:
> > (define (foo ... #:&rest kws)
> >   (apply/kw bar kws))
> > and
> > (apply/kw bar (cons `(#:another-kw ,val) kws))
>
> The advantage of the `...' thing above is that you don't get the
> actual values, so it might be easier to implement things efficiently.

It was actually meant as an elipsis for the lambda head here, not a
special thingie. But it is not a bad idea to utilize it.

> It's unclear whether what you want (which is available now, just more
> verbosely) is worth adding new functionality for -- we might find a
> solution that deals with most cases.

Well, you hinted at the problem yourself in the last message.
It is just painful to use when you are trying to compose.
Being able to do the following would be nice (and useful):

(define (folding f #:direction dir #:iv iv ...) ; perhaps more kws in ...
  ...)
(define (foldl f #:&rest rest)
  (apply/kw folding f #:direction 'left rest))

The accidental '...' form can indeed make it easy to implement
effeciently, by capturing only (unconsumed?) keywords
(instead of the [e|c|...]lisp &rest behavior of #:&rest above):

(define (foldl f ... kws . args) ; here ... intended as special form
 (apply/kw folding `((#:direction . left) . ,kws) f args))

this assumes the kws bound to an alist, but other forms may be better.
In fact apply/kw can be a macro that expansion-time sorts the extra
keywords and then merges with kws at runtime to pass to keyword-apply.

> (The old `mzlib/kw' library made
> just about anything possible, and many features where never used in
> practice, AFAICT.)
>

True that, but I am not suggesting reaching that level of cruft again.

> In any case, if you run into such problems (in practice, not
> pre-cooked examples), feel free to email (the list, or me directly).
> It will help in looking for a good solution.

Sure - it hasn't reached the threshold of pain yet, but I can see it
coming further down the road.

-- vyzo



Posted on the users mailing list.