[mercury-users] wrapper types

Ralph Becket rafe at cs.mu.OZ.AU
Mon Dec 2 08:41:36 AEDT 2002


Michael Day, Friday, 29 November 2002:
> 
> Rather than duplicate the functionality of input_stream or XML element, in 
> many cases it is easier to create wrapper types that provide merely the 
> additional functionality that is needed.
> 
> :- type putback_input_stream(T).
> :- instance input(putback_input_stream(T)) <= input(T).
> :- instance putback(putback_input_stream(T)) <= input(T).
> 
> % use putback_input_stream(input_stream)
> 
> This is fine until more than one such wrapper is involved and the number
> of type classes increases, in which case the lack of modularity of the
> approach starts to drag it down. What happens when you want to compose
> functionality such as input, putback, seeking and line numbering? You may
> not want or need all of that functionality all of the time, so making an
> uber type that does all of it is not necessarily the best solution. But a
> stack of many wrapper types becomes unmanageable, as each one must forward
> operations it doesn't support to the one underneath, and the types must be
> kept in some kind of consistent wrapping order.

One solution would be to introduce default method implementations,
which would relieve the programmer of writing all that method-forwarding
boiler plate.

Other than that, I think you're stuck: if your type classes form an
hierarchy then you only need to define instancehood of the appropriate
leaf classes.

If your type classes do not form an hierarchy, then you must define
instancehood on a case-by-case basis.

Even in statically typed OO languages this is the case.  You don't need
to do this for languages like Python, but that's because Python's method
invocation is dynamic and by name.  Slooooow and unsafe.

- Ralph
--------------------------------------------------------------------------
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