[plt-scheme] passing-on keyword arguments
On Mar 18, Dimitris Vyzovitis wrote:
> 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))
(What would help is "real" examples of such needs.)
> 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 is problematic in a similar way to #:&rest -- there are two
separate things that can get captured -- the keyword-values and the
other arguments. That's why I like the `...' (as I mentioned earlier,
not the above) -- it allows capturing these things and using them
implicitly, without the usual problems. (BTW, these problems include
the fact that the keywords that are passed to keyword-apply should be
sorted etc.)
> 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.
Right -- and you can't just have any random expression there.
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!