Path: utzoo!utgpu!water!watmath!clyde!ima!think!barmar From: barmar@think.COM (Barry Margolin) Newsgroups: comp.lang.lisp Subject: Re: Are optional arguments required? Keywords: CommonLISP, optional and keyword arguments. Message-ID: <25537@think.UUCP> Date: 11 Aug 88 17:25:13 GMT References: <15115@shemp.CS.UCLA.EDU> Sender: usenet@think.UUCP Reply-To: barmar@kulla.think.com.UUCP (Barry Margolin) Distribution: na Organization: Thinking Machines Corporation, Cambridge, MA Lines: 52 In article <15115@shemp.CS.UCLA.EDU> leo@CS.UCLA.EDU (Leonid V. Belyaev) writes: > (defun test-args (req1 &optional (opt1 'opt1) > &key (key1 'key1) > (key2 'key2)) > >Seems fine so far. Now, when you call TEST-ARGS with arguments > > 1 2 :key1 3 > >everything is fine. However, arguments > > 1 :key1 2 > >result in an error. This is true. All "positional" arguments must be specified before any keyword arguments. The reason is that a keyword is a perfectly valid value for an optional argument. So, in your second call, REQ1 is given the value 1, OPT1 is given the value :KEY1, and then it tries to parse the rest of the arguments as keyword arguments, which results in the error you got. There's no way to get rid of this restriction except by making keywords invalid as values for optional arguments, which is a very unlikely thing to happen. >However, doesn't this defeat the purpose for &optional arguments? Not really. Consider a function that has two optional arguments. You can't supply the second optional argument without supplying the first. This doesn't defeat the purpose of making the first one optional, although it is somewhat less convenient. The programmer has decided that a caller that needs to specify the second optional argument is likely to want to specify the first, too. Think of the keywords as a set of "last" optional arguments, which may be in any order among themselves. In general, I think it is poor style to have both &optional and &key arguments. Any function that has more than about three optional arguments should probably make them all &key. The whole point of &key arguments is to solve the problem where the caller only wants to specify some of the optional arguments. Barry Margolin Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar