This is the mail archive of the guile@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] |
Last night I was thinking how convenient the optional argument facilities in the C interface to guile are, and how useful such a thing would be for procedures defined in scheme. So I hacked up a set of macros that do this, and provide a pretty nice syntax, I think. To add optional arguments to a procedure, you use lambda*, define* or define*-public in place of the normal version and place the optional arguments inside brackets at the end of the argument list, before a possible rest argument. For example: (lambda* (a b [c d] . e) '()) creates a procedure with fixed arguments a and b, optional arguments c and d, and rest argument e. If the optional arguments are omitted in a call, the variables for them are unbound in the procedure. This can be checked with the bound? macro. Optional arguments can also be given default values which they take on when they are not present in a call, by giving a two-item list in place of an optional argument, for example in: (lambda* (foo [(bar 42)]) (list foo bar)) foo is a fixed argument and bar is an argument with default value 42. Default value expressions are not evaluated unless they are needed and until the procedure is called. define* and define*-public work just like the non-* versions, but they desugar to lambda* where the normal versions would use lambda. I've uploaded my optargs.scm to the incoming directory in the contrib section on ftp.red-bean.com, and its also available at http://web.mit.edu/www/optargs.scm http://web.mit.edu/www/optest.scm has an example of how to use the optargs module. It just needs to be dropped into /usr/share/guile/util/optargs.scm (or the analogous place in your load path.) I think my macros are pretty well written and provide a very useful feature with a nice syntax, and I'd be glad to see this go in the core guile distribution. I'll gladly transfer copyright to the FSF if someone will tell me how. [side note: these macros use brackets as syntax. R4RS says brackets are supposed to be flagged by the reader, since they are "reserved for future extensions to the language." Since guile accepts bracketsin symbols anyway but since actually using them in symbols is very non-portable, I figure their use in this module won't hurt too much.] - Maciej Stachowiak