FW: [mercury-users] Records

Michael Day mikeday at corplink.com.au
Tue Nov 2 22:28:59 AEDT 1999


> * read-only fields
> * private fields

Hmm perhaps I misinterpreted the post, but it seemed to be a mere
syntactical addition to allow the deconstruction of compound terms using
indexing by name rather than position. For example:

:- type triple(T) ---> triple(first :: T, second :: T, third :: T).

:- pred second_position_in_triple(triple(T), T).
:- mode second_position_in_triple(in, out) is det.

% accesses the second item in a triple, indexing by position

second_position_in_triple(triple(_, Y, _), Y).

:- pred second_field_in_triple(triple(T), T).
:- mode second_field_in_triple(in, out) is det.

% accesses the second item in a triple, indexing by name

second_field_in_triple(X, X ^ second).


That kind of syntactical magic seems reasonable, and allows you to
reshuffle the order of items in large structures without breaking too much
code. Making some items in triple "read-only" doesn't seem to make much
sense, though it depends what use you had in mind. Isn't every logical
variable effectively read only? Using the new "assignment" syntax is still
not changing anything, it's just making a copy of a structure with one of
the fields changed, as a shorthand for the current more laborious way of
phrasing it:

:- pred zero_second_position_in_triple(triple(int), triple(int)).
:- mode zero_second_position_in_triple(in, out) is det.

zero_second_position_in_triple(triple(X, _, Z), triple(X, 0, Z)).

:- pred zero_second_field_in_triple(triple(int), triple(int)).
:- mode zero_second_field_in_triple(in, out) is det.

zero_second_field_in_triple(X, X ^ second := 0).


Actually I'm not sure if that's valid code under the new record regime,
but it's something along those lines.

Indexing by name appears occasionally useful, and "assignment" by name
somewhat odd, but I'd be happy at least to be able to give more
authorative names to structure elements than just putting them in a
comment next to the definition. Surely debuggers and automatic
documentation-from-code generators can do more with field names than
parsing comments.

Michael


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