[mercury-users] Optional fields in a datatype - how to represent?
Terrence Brannon
princepawn at earthlink.net
Mon Apr 23 10:58:28 AEST 2001
Fergus Henderson writes:
> 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?
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.
1- And what happens if you attempt to access it and it is not bound.
>
> --
> 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
> --------------------------------------------------------------------------
--------------------------------------------------------------------------
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