[mercury-users] Records

Simon Taylor stayl at cs.mu.OZ.AU
Tue Nov 2 17:35:04 AEDT 1999


 
> I've seen on the developers' list that Mercury is soon to gain data
> structures with named fields (i.e. records).  Is there a document
> around giving any details?  I'd really like to take a peek...

The syntax for records hasn't been finalised yet.
I have code which implements the following syntax.
Any feedback would be appreciated.

:- type t
	---> t(
		field1 :: int,
		field2 :: string,
		field3 :: t2
	).

:- type t2
	---> t2(
		field4 :: float,
		field5 :: char
	).


foo(X) :-
	X0 = t(1, "abc", t2(1.5, 'y')),

	% field extraction.
	Field1 = X ^ field1,		% Field1 = 1.
	Field5 = X ^ field3 ^ field5,	% Field5 = 'y'.

	% field update.
	X1 = X0 ^ field1 := 2,		% X1 = t(2, "abc", t2(1.5, 'y')).
	X = X1 ^ field3 ^ field5 := 'z'.
					% X2 = t(2, "abc", t2(1.5, 'z')).


% DCG example:
:- pred bar(t::in, t::out) is det.
bar -->
	Field1 := ^ field1,
	Field5 := ^ field3 ^ field5,

	^ field1 := 2,
	^ field3 ^ field5 := 'z'.

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