[mercury-users] Record Accessors

ChrisRath at aol.com ChrisRath at aol.com
Sat Oct 14 15:19:42 AEDT 2000


Question from a complete newbie to the world of Mercury programming.

I was trying to figure out how to access individual fields within a declared 
record type.  The reference manual seems to point to using Var^field as 
syntactical sugar for returning the value contained - effectively making it a 
get accessor.  But I can't seem to figure out how it works.  Below is a 
simple program that demonstrates what I am trying to do.  The getID function 
is supposed to return the employee ID or the record.  Unfortunately, I get a 
compile error approximately saying that the arity of the function for ^ 
should be (int, int).  

I was wondering whether the record accessor (^) is a recent addition that I 
don't have (I am running Linux version 0.9.1)?  Or am I missing the obvious?  
Or is there another way to extract the value of the field from the record?

Thanks.  Test program follows...

:- module test.
:- interface.
:- import_module io, list, int.
:- pred main(io__state, io__state).
:- mode main(di, uo) is det.
:- implementation.

main -->
   { Emp = employee(123, "Chris") },
   io__write_string("EmployeeID = "),
   io__write_int(getID(Emp)),
   io__nl.

:- type employee 
   ---> employee(
      empid :: int,
      name :: string
   ).

:- func getID(employee) = int.
:- mode getID(in) = out is det.

% this is how I thought the field could be accessed
% getID(X) = X^empid.

% this is temp workaround to make sure everything else is ok.
getID(X) = 123.
--------------------------------------------------------------------------
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