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

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Jun 12 23:34:48 EDT 2007

On Jun 13, Matthew Flatt wrote:
> 
> It's analogous to the difference between these two:
> 
>   (lambda args (unless (= (length args) 1) (error "no")) ....)
>   (lambda (a) ....)
> 
> The difference between these is visible via `procedure-arity'.

This is a very good analogy.


> In the keyword case, here's how the two different things would be
> written:
> 
>  (lambda (#:y y) ....)                    ; required
>  (lambda (#:y [y (error "no y!")]) ....)  ; "optional"
> 
> With the former, applying it to no arguments triggers an exception
> about a missing `#:y' argument, or at least "no matching case". With
> the latter, applying it to no arguments triggers an exception with
> "no y!". That's not much of a difference.
> 
> But `keyword-procedure-arity' would say that the former requires a
> #:y argument, while it would say that the latter optionally accepts
> a #:y argument.

And there's more than that -- when "kw.ss" has it's own
default-to-error for keywords (which I still have on my todo list as a
new `#:named' alternative to `#:key'), then the code should be able to
detect procedures with keywords that are all required, like

  (lambda (#:x x #:y y #:z z) ...)

and make it so that (when possible) calls to this function are
compiled to plain function -- which gives you exactly the same
functionality as OCaml's named arguments.  (Which is the reason for
the `#:named'.)


> In fact, maybe `procedure?' reports #f for the first and #t for the
> second. (This design point is still open, I think.)

(I don't think I like `procedure?' returning #f for an applicable
value...  But IMO the other problem needs some resolution before this
is decided.)

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


Posted on the users mailing list.