[m-dev.] io streams
David Glen JEFFERY
dgj at cs.mu.OZ.AU
Tue Jan 4 14:03:54 AEDT 2000
On 28-Dec-1999, David Overton <dmo at hydra.cs.mu.oz.au> wrote:
>
> Perhaps this is one situation where default methods would be nice: the
> default would define read_chars in terms of read_char, but individual
> instances could redefine it if it is possible to write a more
> efficient version.
Yes, but one can achieve roughly the same thing as follows:
:- typeclass stream__input(S) <= stream__stream(S) where
[
pred stream__read_char(stream__result(char), S, S),
mode stream__read_char(out, di, uo) is det,
pred stream__read_chars(stream__result(list(char)), int, S, S),
mode stream__read_chars(out, in, di, uo) is det
...
].
:- pred default_read_chars(stream__result(list(char)), int, S, S)
<= stream__input(S).
:- mode default_read_chars(out, di, uo) is det.
default_read_chars(Cs) -->
read_char(C),
% etc.
Then, in each instance declaration where you want to use the default, you
can just say:
:- instance stream__input(mytype) where [
pred(read_char/3) is mytype_read_char,
pred(read_chars/3) is default_read_chars
].
This is perhaps a minor inconvenience, but also arguably a good way of doing
things.
dgj
--
David Jeffery (dgj at cs.mu.oz.au) | If your thesis is utterly vacuous
PhD student, | Use first-order predicate calculus.
Dept. of Comp. Sci. & Soft. Eng.| With sufficient formality
The University of Melbourne | The sheerist banality
Australia | Will be hailed by the critics: "Miraculous!"
| -- Anon.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list