Puzzled by a declaration

Discussion of Common Lisp
Post Reply
sabra.crolleton
Posts: 16
Joined: Sat Sep 13, 2008 6:46 pm

Puzzled by a declaration

Post by sabra.crolleton » Mon Oct 04, 2010 10:26 pm

I was looking at the source code for cl-dot and came across the functions defined below. In the first function, why declare 2 key parameters after the &rest , then declare they should be ignored, then pass the &rest parameter to a function that doesn't seem to use it, but rather wants the parameters which were declared ignore?

Sabra

Code: Select all

(defun print-graph (graph &rest options
                    &key (stream *standard-output*) (directed t))
  "Prints a dot-format representation GRAPH to STREAM."
  (declare (ignore stream directed))
  (apply #'generate-dot
         (nodes-of graph)
         (edges-of graph)
         (attributes-of graph)
         options))

(defun generate-dot (nodes edges attributes
                     &key (stream *standard-output*) (directed t))
....)

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Puzzled by a declaration

Post by ramarren » Tue Oct 05, 2010 12:29 am

My guess would be that it is a result of refactoring which was not properly cleaned up. Probably the interface to GENERATE-DOT changed, and PRINT-GRAPH was only minimally updated.

In general such a pattern would be useful if you wanted to give default values to some, but not all, key arguments in the wrapping function, and then pass them all to the wrapped function. In this case since the latter takes no additional arguments there is no much point, but if it did, there might be. It could also save some typing since you would not have to retype the key arguments keys and values in the inner call, but for two arguments it is not significant.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Puzzled by a declaration

Post by gugamilare » Tue Oct 05, 2010 1:17 pm

I don't think this is a refactoring without properly clean up, the author intends to be able to extend the function generate-dots to accept more key arguments in the future without having to change the functions that call it.

If you create a lot of functions with fixed required / key parameters, when you have a considerable amount of code, you might need to do a lot of refactoring everywhere to make a small change. Yup, that happened to me already, so, it is interesting in certain cases to let the key parameters free and pass around those parameters to other functions.

Post Reply