[mercury-users] design of stream library

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Sep 4 22:13:21 AEDT 2000


On 04-Sep-2000, Peter Ross <peter.ross at miscrit.be> wrote:
> :- typeclass stream__output(S) where [
>     pred write_char(S::in, char::in, stream(S)::di, stream(S)::uo) is det
> ].
> 
> The problem with this design is that it doesn't work very well with
> strings.  Under the old design you could write:
> 
>     write_char('x', "initial string", String)
> 
> but now you would need something like:
> 
>     string_stream_init("initial string", Handle, Stream),
>     write_char(Handle, 'x', Stream0, Stream),
>     get_string_from_string_stream(Hanlde, Stream, String)
> 
> which isn't very nice.

Well, you could use

	String = build_string("initial string", (pred(Handle::in, di, uo) -->
		write_char(Handle, 'x')
	)

which is a bit nicer.

Here build_string/2 would be a library predicate defined by

	build_string(Initial, StreamActions) = String :-
		string_stream_init(Initial, Handle, Stream0),
		StreamActions(Handle, Stream0, Stream),
		get_string_from_string_stream(Handle, Stream, String).

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list