FW: [mercury-users] Records

Peter Schachte schachte at cs.mu.OZ.AU
Mon Nov 8 14:17:01 AEDT 1999


On Mon, Nov 08, 1999 at 10:52:24AM +1100, Simon Taylor wrote:
> > 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.

Can you give an example?  As far as I can see, all that'll happen if
you spell the field name differently in the definition and use is that
you'll get a complaint about an unknown function.  If the code that
does the X ^ F ==> F(X) translation does some checking, it could even
give a more helpful message.

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

OK.

Actually, I still think it would be better if the get function just
had the field name as its name.  I've always found `get_foo' kind of
unwieldy and procedural looking.  Eg, we could define lists has having
two fields: head and tail.  Then users could write L^head or head(L)
interchangably, using whichever notation seemed more appropriate to
them.  Also, it would make perfect sense to pass a member access
function as argument to a higher-order function.  In such cases it
would look much nicer to just pass a member name, rather than
something with 'get ' or get_ prepended.

Basically, I'm arguing that we should look at member access as just a
function call.  Naming a field just specifies a name for an
automatically-generated deconstructor function, and ^ becomes just
another syntax for function application.

The `:=' syntax is rather more magical, and I have no objection to
pasting 'set ' or set_ (I prefer the former) on the front of the
accessor name to get the setter name.  As Fergus pointed out, accessor
and setter functions need to have different names.  Again, though, I
think one should be able to use the := notation for any function which
has a matching setter function with the right name and signature; it
shouldn't be tied to field access or even the ^ notation for function
application.

> and reporting an error for get and set functions for which there is no
> matching field.

My main point was that you shouldn't do that.  It's very useful to be
able to use the same syntax to access and modify both real and "fake"
fields.  This allows a type with field accessors/setters to still be
an ADT.

> We'd also need a `:- derived_field <fieldname>' declaration.

What's that for?


-- 
Peter Schachte                     A billion here, a couple of billion there
mailto:schachte at cs.mu.OZ.AU        -- first thing you know it adds up to be
http://www.cs.mu.oz.au/~schachte/  real money.
PGP: finger schachte at 128.250.37.3      -- Senator Everett McKinley Dirksen 
--------------------------------------------------------------------------
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