From: Peter Seibel
Subject: Use of special variables as function parameters?
Date: 
Message-ID: <m3n0i2f1lv.fsf@javamonkey.com>
Is it common to use a special variable as an parameter to a function?
I mean something like this:

  (defvar *x* 'whatever)                

  (defun foo (*x*) (stuff-that-uses-x))

or:

  (defun foo (%x%)
    (declare (special %x%))
    (stuff-that-uses-x))

I suppose this kind of construction could be useful for providing an
entry point to some code that rebinds the special variable for you.
But it looks sort of strange to my untrained eye. Are there any common
coding idioms that use this kind of construct?

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra

From: Henrik Motakef
Subject: Re: Use of special variables as function parameters?
Date: 
Message-ID: <87y91lq486.fsf@interim.henrik-motakef.de>
Peter Seibel <·····@javamonkey.com> writes:

> Is it common to use a special variable as an parameter to a function?
> I mean something like this:
>
>   (defvar *x* 'whatever)                
>
>   (defun foo (*x*) (stuff-that-uses-x))
>
> or:
>
>   (defun foo (%x%)
>     (declare (special %x%))
>     (stuff-that-uses-x))
>
> I suppose this kind of construction could be useful for providing an
> entry point to some code that rebinds the special variable for you.
> But it looks sort of strange to my untrained eye. Are there any common
> coding idioms that use this kind of construct?

I don't know if it's what you are looking for, but you frequently see
special variables as the default for &optional/&key arguments, as in

(defun foo (&optional (x *default-x*))
  ...)

WRITE uses this style, for example.

Regards
Henrik
From: Tim Bradshaw
Subject: Re: Use of special variables as function parameters?
Date: 
Message-ID: <ey3ptmx7nxt.fsf@cley.com>
* Peter Seibel wrote:

> I suppose this kind of construction could be useful for providing an
> entry point to some code that rebinds the special variable for you.
> But it looks sort of strange to my untrained eye. Are there any common
> coding idioms that use this kind of construct?

I'd never do that.  If I wanted to rebind a special I'd say:

(defun foo (x)
  (let ((*x* x)) ...))

I don't think there's a practical difference, but the other one makes
me feel pretty bad.

--tim
From: Timothy Moore
Subject: Re: Use of special variables as function parameters?
Date: 
Message-ID: <b9cr5l$8cj$0@216.39.145.192>
Peter Seibel <·····@javamonkey.com> writes:

> Is it common to use a special variable as an parameter to a function?
> I mean something like this:
> 
>   (defvar *x* 'whatever)                
> 
>   (defun foo (*x*) (stuff-that-uses-x))
> 
> or:
> 
>   (defun foo (%x%)
>     (declare (special %x%))
>     (stuff-that-uses-x))
> 
> I suppose this kind of construction could be useful for providing an
> entry point to some code that rebinds the special variable for you.
> But it looks sort of strange to my untrained eye. Are there any common
> coding idioms that use this kind of construct?

It's probably not that common to write this explicitly, but it does
come up with macros that rebind a supplied variable and wrap their
body in a function.  It's especially applicable to input or output
since there are all those special stream variables.  Here's an
abridged example from McCLIM:

(defmacro with-output-as-presentation ((stream object type
				       &rest key-args
				       &key 
				       (record-type ''standard-presentation)
				       &allow-other-keys)
				       &body body)
  (when (eq stream t)
    (setq stream '*standard-output*))
  (let ((output-record (gensym)))
     `(flet ((continuation (,stream ,output-record)
	      (declare (ignore ,output-record))
              ,@body))
	(if (output-recording-stream-p ,stream)
	    (invoke-with-new-output-record
	     ,stream #'continuation ,record-type
	     :object ,object
	     :type (expand-presentation-type-abbreviation ,type)
	     ,@key-args)
	    (funcall #'continuation ,stream nil)))))

Tim