From: rif
Subject: format to nil?
Date: 
Message-ID: <wj0fyxsj7jd.fsf@five-percent-nation.mit.edu>
If I write a function that formats debug output to a passed-in stream
parameter and I'm not interested in seeing that output, I can/should
just pass nil in as the stream parameter?  Is this a common idiom?

rif

From: Zach Beane
Subject: Re: format to nil?
Date: 
Message-ID: <m3fyxsj6is.fsf@unnamed.xach.com>
rif <···@mit.edu> writes:

> If I write a function that formats debug output to a passed-in stream
> parameter and I'm not interested in seeing that output, I can/should
> just pass nil in as the stream parameter?  Is this a common idiom?

If you want to discard output, a stream created with
(MAKE-BROADCAST-STREAM) might be better.

Zach
From: Edi Weitz
Subject: Re: format to nil?
Date: 
Message-ID: <upsww9cf7.fsf@agharta.de>
On 15 Apr 2005 13:04:22 -0400, rif <···@mit.edu> wrote:

> If I write a function that formats debug output to a passed-in
> stream parameter and I'm not interested in seeing that output, I
> can/should just pass nil in as the stream parameter?  Is this a
> common idiom?

  (make-broadcast-stream)

This has the advantage that it actually results in a stream (kind of
like /dev/null in Unix) which can be fed to every function which
accepts a stream for output.  You can't do that with NIL.

Cheers,
Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Pascal Bourguignon
Subject: Re: format to nil?
Date: 
Message-ID: <871x9bapg9.fsf@thalassa.informatimago.com>
rif <···@mit.edu> writes:

> If I write a function that formats debug output to a passed-in stream
> parameter and I'm not interested in seeing that output, I can/should
> just pass nil in as the stream parameter?  

You could.

> Is this a common idiom?

Yes.  But not to ignore it:

(format nil "# ~A" 42) --> "# 42"


I'd do something like:

(defconstant +debug+ nil)
(defmacro when-debug (&body body)  (when +debug+ `(progn ,@body)))
(when-debug (format *trace-output* "n=~A~%" n))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
From: Kent M Pitman
Subject: Re: format to nil?
Date: 
Message-ID: <uvf6n7p08.fsf@nhplace.com>
rif <···@mit.edu> writes:

> If I write a function that formats debug output to a passed-in stream
> parameter and I'm not interested in seeing that output, I can/should
> just pass nil in as the stream parameter?  Is this a common idiom?

It conses needlessly to do that.

For a bit-bucket of output going nowhere (i.e, like writing to /dev/null on
unix/linux), use
 (make-broadcast-stream)
that is, call that function with no arguments, meaning "broadcast to no one".  

On a related note, for an empty input stream, use
 (make-concatenated-stream)
meaning "concatenate no inputs".  You'll get an immediate
eof when you try to read from this.  But it is trivially a stream.
(I think this corresponds to reading from /dev/null, though I've
got less experience doing that.)
From: Rob Warnock
Subject: Re: format to nil?
Date: 
Message-ID: <Auudneetc-M8mvzfRVn-tw@speakeasy.net>
Kent M Pitman  <······@nhplace.com> wrote:
+---------------
| For a bit-bucket of output going nowhere (i.e, like writing to
| /dev/null on unix/linux), use (make-broadcast-stream), that is,
| call that function with no arguments, meaning "broadcast to no one".  
| 
| On a related note, for an empty input stream, use
|  (make-concatenated-stream)
| meaning "concatenate no inputs".  You'll get an immediate
| eof when you try to read from this.  But it is trivially a stream.
| (I think this corresponds to reading from /dev/null, though I've
| got less experience doing that.)
+---------------

Very close, actually. And so if one *really* likes the /dev/null
analogy, one might like this:    ;-}  ;-}

    (make-two-way-stream (make-concatenated-stream)
			 (make-broadcast-stream))


-Rob

p.s. For some reason, CMUCL-19a barfs if you try to write
to such a stream, though writing to this one works fine:

    (make-two-way-stream (make-string-input-stream "")
			 (make-broadcast-stream))

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Raymond Toy
Subject: Re: format to nil?
Date: 
Message-ID: <sxdsm1o5h17.fsf@rtp.ericsson.se>
>>>>> "Rob" == Rob Warnock <····@rpw3.org> writes:

    Rob> Very close, actually. And so if one *really* likes the /dev/null
    Rob> analogy, one might like this:    ;-}  ;-}

    Rob>     (make-two-way-stream (make-concatenated-stream)
    Rob> 			 (make-broadcast-stream))


    Rob> -Rob

    Rob> p.s. For some reason, CMUCL-19a barfs if you try to write
    Rob> to such a stream, though writing to this one works fine:

    Rob>     (make-two-way-stream (make-string-input-stream "")
    Rob> 			 (make-broadcast-stream))

This is odd.  The first version above works for me on cmucl-19a on
sparc, and also works with current CVS versions.

What platform are you running this on?

Ray
From: Rob Warnock
Subject: Re: format to nil?
Date: 
Message-ID: <KPmdnXEt-NY0BvnfRVn-uw@speakeasy.net>
Raymond Toy  <···········@ericsson.com> wrote:
+---------------
| >>>>> "Rob" == Rob Warnock <····@rpw3.org> writes:
|     Rob>     (make-two-way-stream (make-concatenated-stream)
|     Rob> 			 (make-broadcast-stream))
| 
|     Rob> p.s. For some reason, CMUCL-19a barfs if you try to write
|     Rob> to such a stream, though writing to this one works fine:
| 
|     Rob>     (make-two-way-stream (make-string-input-stream "")
|     Rob> 			 (make-broadcast-stream))
| 
| This is odd.  The first version above works for me on cmucl-19a on
| sparc, and also works with current CVS versions.
| What platform are you running this on?
+---------------

Fails: 19a on an Athlon-64 in 32-bit mode under Linux 2.4.21-20.EL
(core was "Dumped on: Wed, 2004-07-28 09:51:48-07:00 on lorien")

Fails: 19a-pre3 on a P-4 running Linux 2.4.21-15.EL
(core "Dumped on: Wed, 2004-06-30 13:13:25-04:00 on lorien").

Fails: 19a-pre3 FreeBSD prebuilt (at SRI) binary on an Athlon-XP (FBSD-4.6)
("Dumped on: Fri, 2004-07-02 11:56:53-07:00 on snapdragon.csl.sri.com").

Works fine: 18e FreeBSD binary on an Athlon-XP (FBSD-4.6)
("Dumped on: Thu, 2003-04-03 10:07:59-08:00 on cvs2.cons.org"),

Test case for all the above:

    (let ((s (make-two-way-stream (make-concatenated-stream)
				  (make-broadcast-stream))))
      (dotimes (i 10) (format s "Hello, i = ~a~%" i)))

"Works" simply returns NIL.

"Fails" gives some variant of this:

    Type-error in KERNEL::OBJECT-NOT-TYPE-ERROR-HANDLER:
       NIL is not of type FUNDAMENTAL-STREAM
       [Condition of type TYPE-ERROR]

    Restarts:
      0: [ABORT] Return to Top-Level.

    Debug  (type H for help)

    (LISP::STREAM-MISC-DISPATCH 4 NIL :LINE-LENGTH NIL ...)[:EXTERNAL]
    Source: Error finding source: 
    Error in function DEBUG::GET-FILE-TOP-LEVEL-FORM:  Source file no longer exists:
      target:code/stream.lisp.
    0] back

    0: (LISP::STREAM-MISC-DISPATCH 4 NIL :LINE-LENGTH NIL ...)[:EXTERNAL]
    1: (LISP::TWO-WAY-MISC
	#<Two-Way Stream, Input = #<Concatenated Stream, Streams = NIL>, Output = #<Broadcast Stream>>
	:LINE-LENGTH
	NIL
	NIL)
    2: (PRETTY-PRINT::MAKE-PRETTY-STREAM
	#<Two-Way Stream, Input = #<Concatenated Stream, Streams = NIL>, Output = #<Broadcast Stream>>)
    3: (PRETTY-PRINT::OUTPUT-PRETTY-OBJECT 0
					   #<Two-Way Stream, Input = #<Concatenated Stream, Streams = NIL>, Output = #<Broadcast Stream>>)
    4: (PRINC 0
	      #<Two-Way Stream, Input = #<Concatenated Stream, Streams = NIL>, Output = #<Broadcast Stream>>)
    5: (FORMAT::A-FORMAT-DIRECTIVE-INTERPRETER
	#<Two-Way Stream, Input = #<Concatenated Stream, Streams = NIL>, Output = #<Broadcast Stream>>
	#<~a>
	(#<~%>)
	#<unused-arg>
	...)
    ...[remainder omitted for brevity]...


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Raymond Toy
Subject: Re: format to nil?
Date: 
Message-ID: <sxdd5sq4uah.fsf@rtp.ericsson.se>
>>>>> "Rob" == Rob Warnock <····@rpw3.org> writes:

    Rob> Test case for all the above:

    Rob>     (let ((s (make-two-way-stream (make-concatenated-stream)
    Rob> 				  (make-broadcast-stream))))
    Rob>       (dotimes (i 10) (format s "Hello, i = ~a~%" i)))


Thanks for the test example.  I can reproduce it now.

Ray