This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Keyword arguments


I suggest this is added to the FAQ somewhere:

Q:

How do I create a procedure that takes keyword arguments? Such
that both calls are legal (and work):

(my-proc "test" #:foo 1 #:bar 2)
(my-proc "test" #:bar 0)

A:

guile> (use-modules (ice-9 optargs))
guile> (define my-proc (lambda* (req #&key foo bar)
...   (display "req = ") (display req) (newline)
...   (if (bound? foo) (begin (display "foo = ") (display foo)
(newline)))
...   (if (bound? bar) (begin (display "bar = ") (display bar)
(newline)))
... )
... )
guile> (my-proc 1)
1
guile> (my-proc 1 2)
1
guile> (my-proc 1 #:foo 2)
1
foo = 2
guile> (my-proc 1 #:bar 2)
1
bar = 2
guile> (my-proc 1 #:bar 2 #:foo 3)
1
foo = 3
bar = 2
guile> (my-proc 1 #:bar 2 #:foo 3 #:bar 0)
1
foo = 3
bar = 0



Still, I think it would be useful if I could freely intermix
keyword and non-keyword arguments in the call, like
  (my-proc #:foo 1 #:bar 2 "test")
I can use a "rest" parameter, but optargs will fill it with the
keywords too, which is not good. For CDH I'm using the ugly
workaround of using the last element from "rest" and ignoring
anything else...

[]s,
                                               |alo
                                               +----
--
          Hack and Roll  ( http://www.hackandroll.org )
            News for, uh, whatever it is that we are.


http://www.webcom.com/lalo           mailto:lalo@hackandroll.org
                 pgp key in the personal page

Debian GNU/Linux            ---            http://www.debian.org
Brazil of Darkness        ---       http://zope.gf.com.br/BroDar


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]