[mercury-users] Optional fields in a datatype - how to represent?

David Overton dmo at cs.mu.OZ.AU
Mon Apr 23 11:37:08 AEST 2001


On Sun, Apr 22, 2001 at 05:58:28PM -0700, Terrence Brannon wrote:
> Fergus Henderson writes:
>  > 	:- func fred = employee.
>  > 	fred = employee("Fred", 33, no).
>  > 
>  > 	:- func jerry = employee.
>  > 	jerry = employee("Jerry", 42, yes("Marketing")).
>  > 
>  > Does that answer your question?
> 
> Yes. 
> 0- that is something I haven't seen before. each instance of employee
> is a function. how do you access the fields of each instance? I saw
> the field access section of the reference manual, but it doesn't show
> how to access fields when the type instance is a function.

You can access the fields the same way as you would for any other
value of the type.  E.g. to access fred's age, either do a
deconstruction:

	fred = employee(_, Age, _)

or, use field access syntax:

	Age = fred ^ age

BTW, it's often helpful to think of a zero arity function as just a
constant, so 'fred' 'jerry' are just constants of type 'employee'.  

> 
> 1- And what happens if you attempt to access it and it is not bound.
> 

If you access a field that is not bound and try to use it somewhere
that is expecting a bound value you will get a compile-time mode
error.


David
-- 
David Overton      Department of Computer Science & Software Engineering
PhD Student        The University of Melbourne, Victoria 3010, Australia
+61 3 8344 9159    http://www.cs.mu.oz.au/~dmo
--------------------------------------------------------------------------
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