On Mon, Jun 20, 2016 at 10:14:52PM -0400, Michael Silver wrote:
> > Alternatively, we could decide to do this for CHICKEN 5 only, and rename
> > both procedures to match the Guile naming convention.  Maybe do a survey
> > of existing implementations that offer such a procedure and see what names
> > they use.
> 
> Maybe, but even coming from someone who is porting a huge Guile codebase to 
> CHICKEN, I don’t mind CHICKEN having a different naming for the function. 
> Find and replace is easy :)

Scheme xref says with-error-to-port is supported by Gauche, SCM and Stk:
http://practical-scheme.net/wiliki/schemexref.cgi?with-error-to-port
versus ours, which is only in CHICKEN:
http://practical-scheme.net/wiliki/schemexref.cgi?with-error-output-to-port

I don't know how complete the xref is.  Obviously it's not comprehensive,
because it doesn't mention Guile on the page about with-error-to-string.
But it seems to have the most important implementations.

> > But it's all rather arbitrary: why is there no with-error-to-file?
> > And if there were, it would be jarring to have to (import scheme) to
> > get with-output-to-file, but have to do (import (chicken ports)) to get
> > with-error-to-file…
> 
> Makes sense, but since with-output-to-string requires importing ports, I 
> don’t think its unreasonable to ask for the same for it’s sister function 
> with-error-output-to-string. It makes as much sense to group those two 
> functions together in one unit as it does to group with-output-to-port and 
> with-error-output-to-port, which seems totally fine to me. The ports unit 
> doesn’t seem to have anything relating to file output, but it does have both 
> output and error output to ports, and output to string, but misses error 
> output to string. Looked like an obvious “what’s missing from the pattern” to 
> me, which is why I suggested the patch.

Yeah, it makes the most sense to at least make our own extensions to
the standard internally consistent, even if the standard itself is not
complete in this aspect (r5rs doesn't have current-error-port, r7rs does
have current-error-port but, oddly, no procedures to manipulate it).

Maybe we should add with-error-to-file to chicken.base, or whereever we
put current-error-port?

The attached patch adds with-error-to-string and renames
with-error-output-to-port to with-error-to-port, for chicken-5 only.
We probably shouldn't be adding new procedures to CHICKEN 4 at this
point: any CHICKEN 4 egg that used this would be depending on exactly
one minor release of CHICKEN that supports this.

The patch also tweaks types.db a bit to improve the type for the
*-to-string procedures, indicating they always return one value,
which is always a string.

Thanks for your suggestion, Michael!

Cheers,
Peter
From 8562617c9c4d3edbda364d44018fb7f86ff5680c Mon Sep 17 00:00:00 2001
From: Peter Bex <[email protected]>
Date: Wed, 22 Jun 2016 20:49:54 +0200
Subject: [PATCH] Add with-error-to-string and rename with-error-output-to-port
 to with-error-to-port

This improves compatibility with Bigloo, Stk, Gauche, SCM and Guile.
---
 NEWS              |  3 +++
 manual/Unit ports | 12 ++++++++++--
 ports.scm         | 22 ++++++++++++++--------
 types.db          |  5 +++--
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/NEWS b/NEWS
index 31b3270..8436d1e 100644
--- a/NEWS
+++ b/NEWS
@@ -28,6 +28,9 @@
   - `sleep` now suspends the current thread when threading is enabled,
     otherwise it sleeps the process. The new `process-sleep` procedure
     in unit posix can be used to sleep the process unconditionally.
+  - `with-error-output-to-port' from the ports module has been renamed
+    to the more common `with-error-to-port', and `with-error-to-string'
+    has been added for completeness (thanks to Michael Silver).
 
 - Module system
   - The compiler has been modularised, for improved namespacing.  This
diff --git a/manual/Unit ports b/manual/Unit ports
index fa3c5e5..9e4d22e 100644
--- a/manual/Unit ports	
+++ b/manual/Unit ports	
@@ -72,9 +72,9 @@ be a procedure of no arguments. {{FLUSH}} (if provided) is called
 for flushing the output port.
 
 
-==== with-error-output-to-port
+==== with-error-to-port
 
-<procedure>(with-error-output-to-port PORT THUNK)</procedure>
+<procedure>(with-error-to-port PORT THUNK)</procedure>
 
 Call procedure {{THUNK}} with the current error output-port
 temporarily bound to {{PORT}}.
@@ -121,6 +121,14 @@ bound to an input-string-port with the contents of {{STRING}}.
 Call procedure {{THUNK}} with the current output-port temporarily
 bound to a string-output-port and return the accumulated output string.
 
+==== with-error-to-string
+
+<procedure>(with-error-to-string THUNK)</procedure>
+
+Call procedure {{THUNK}} with the current error output-port
+temporarily bound to a string-output-port and return the accumulated
+output string.
+
 
 === Port iterators
 
diff --git a/ports.scm b/ports.scm
index 7bb4479..36a03e3 100644
--- a/ports.scm
+++ b/ports.scm
@@ -47,11 +47,12 @@
    port-fold
    make-broadcast-port
    make-concatenated-port
-   with-error-output-to-port
+   with-error-to-port
    with-input-from-port
    with-input-from-string
    with-output-to-port
-   with-output-to-string)
+   with-output-to-string
+   with-error-to-string)
 
 (import scheme chicken)
 (import chicken.io)
@@ -183,17 +184,17 @@
 
 (define (with-input-from-port port thunk)
   (##sys#check-input-port port #t 'with-input-from-port)
-  (fluid-let ([##sys#standard-input port])
+  (fluid-let ((##sys#standard-input port))
     (thunk) ) )
 
 (define (with-output-to-port port thunk)
   (##sys#check-output-port port #t 'with-output-to-port)
-  (fluid-let ([##sys#standard-output port])
+  (fluid-let ((##sys#standard-output port))
     (thunk) ) )
 
-(define (with-error-output-to-port port thunk)
-  (##sys#check-output-port port #t 'with-error-output-to-port)
-  (fluid-let ([##sys#standard-error port])
+(define (with-error-to-port port thunk)
+  (##sys#check-output-port port #t 'with-error-to-port)
+  (fluid-let ((##sys#standard-error port))
     (thunk) ) )
 
 ;;; Extended string-port operations:
@@ -216,10 +217,15 @@
 
 (define with-output-to-string
   (lambda (thunk)
-    (fluid-let ([##sys#standard-output (open-output-string)])
+    (fluid-let ((##sys#standard-output (open-output-string)))
       (thunk) 
       (get-output-string ##sys#standard-output) ) ) )
 
+(define with-error-to-string
+  (lambda (thunk)
+    (fluid-let ((##sys#standard-error (open-output-string)))
+      (thunk) 
+      (get-output-string ##sys#standard-error) ) ) )
 
 ;;; Custom ports:
 ;
diff --git a/types.db b/types.db
index 1215d0d..d2b2b0b 100644
--- a/types.db
+++ b/types.db
@@ -1847,11 +1847,12 @@
 (chicken.ports#port-fold (#(procedure #:enforce) chicken.ports#port-fold ((procedure (* *) *) * (procedure () *)) *))
 (chicken.ports#make-broadcast-port (#(procedure #:clean #:enforce) chicken.ports#make-broadcast-port (#!rest output-port) output-port))
 (chicken.ports#make-concatenated-port (#(procedure #:clean #:enforce) chicken.ports#make-concatenated-port (port #!rest input-port) input-port))
-(chicken.ports#with-error-output-to-port (#(procedure #:enforce) chicken.ports#with-error-output-to-port (output-port (procedure () . *)) . *))
+(chicken.ports#with-error-to-port (#(procedure #:enforce) chicken.ports#with-error-to-port (output-port (procedure () . *)) . *))
 (chicken.ports#with-input-from-port (#(procedure #:enforce) chicken.ports#with-input-from-port (input-port (procedure () . *)) . *))
 (chicken.ports#with-input-from-string (#(procedure #:enforce) chicken.ports#with-input-from-string (string (procedure () . *)) . *))
 (chicken.ports#with-output-to-port (#(procedure #:enforce) chicken.ports#with-output-to-port (output-port (procedure () . *)) . *))
-(chicken.ports#with-output-to-string (#(procedure #:enforce) chicken.ports#with-output-to-string ((procedure () . *)) . *))
+(chicken.ports#with-output-to-string (#(procedure #:enforce) chicken.ports#with-output-to-string ((procedure () . *)) string))
+(chicken.ports#with-error-to-string (#(procedure #:enforce) chicken.ports#with-error-to-string ((procedure () . *)) string))
 
 ;; errno
 
-- 
2.1.4

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to