[mercury-users] Optional fields in a datatype - how to represent?
Fergus Henderson
fjh at cs.mu.OZ.AU
Mon Apr 23 06:52:19 AEST 2001
On 22-Apr-2001, Terrence Brannon <princepawn at earthlink.net> wrote:
> :- type employee
> ---> employee(
> name :: string,
> age :: int,
> department :: string
> ).
...
> what if a field is optional?
> Am I required to supply an empty value for this field?
The usual way of representing optional fields in Mercury
is to use the `maybe' type from the `std_util' module:
:- type maybe(T)
---> yes(T)
; no.
For example, if you want the `department' field to be
optional, you can write
:- type employee
---> employee(
name :: string,
age :: int,
department :: maybe(string),
).
Then when constructing a new employee, you will supply either
`yes("some department name")' or `no' for the department
field, e.g.
:- func fred = employee.
fred = employee("Fred", 33, no).
:- func jerry = employee.
jerry = employee("Jerry", 42, yes("Marketing")).
Does that answer your question?
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
| of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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