FW: [mercury-users] Records

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Nov 5 18:03:30 AEDT 1999


On 05-Nov-1999, Peter Schachte <schachte at cs.mu.OZ.AU> wrote:
> I think there's a simpler way to achieve what Simon is after,
> and provide some other benefits along the way.  Let's consider
> semantics first, and then I'll sprinkle on a little syntactic sugar.
> 
> For a type t with a field f, where f has type v, an accessor for f is
> really nothing but a function f: t -> v, and a setter is just a
> function f: t -> v -> t (I'm using Mercury's overloading ability here
> to let me use the same name for the accessor and setter).

Overloading is not a panacea; it should not be overused.

For example, if you overload `f' as both the accessor and the setter,
then in examples such as the following

	:- pred p(t::in, io__state::di, io__state::uo) is det.
	p(X) --> io__print(f(X)).

the code `print(f(X))' is ambiguous, because `f(X)' could either have
type `v' or type `func(v) = t'.  The latter possibility arises
because `f(X)' could be a partial application of the setter function.
To resolve the ambiguity, an explicit type qualifier would be needed.

To avoid the need for explicit type qualifiers in situations like
that, I think it would be a better idea to use `get_<fieldname>'
and `set_<fieldname>' (e.g. `get_f' and `set_f'),
or perhaps just `<fieldname>' and `set_<fieldname>'.

> My proposal is to just let it go at that: use ordinary functions as
> accessors and setters, and have field names in type declarations just
> generate those functions automatically.  If the type declaration is in
> an interface section, the generated functions are public, otherwise
> not.  But one can always put a type declaration in the implementation
> section, and add function declarations for (some of) the accessors and
> setters in the interface section.  Thus issues of public and private
> can be handled as usual by the module system (as it should be).
> Removing a field from a type can be handled by just defining a new
> public function with the right name.

I think that is an excellent proposal.

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