FW: [mercury-users] Records

Simon Taylor stayl at cs.mu.OZ.AU
Mon Nov 8 10:52:24 AEDT 1999


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

The only complaint I have with using magic function names for which
definitions can be automatically generated by the compiler is that
typos can result in difficult to spot bugs where the programmer's
version is not called.

There are lots of predicates in the Mercury compiler with names
starting with 'get_' that don't have anything to do with field access.
I'd suggest using names of the form 'get <fieldname>' and 'set <fieldname>',
and reporting an error for get and set functions for which there is no
matching field.
We'd also need a `:- derived_field <fieldname>' declaration.
 
> > 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.

One nice consequence of using magic function names like this is
that declaring typeclasses with fields is easy.

:- derived_field field.
:- typeclass has_field(T) where [
		func 'get field'(T) = int,
		func 'set field'(T, int) = T
	].

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