[mercury-users] Working with exported types
Jonathan Morgan
jonmmorgan at gmail.com
Thu Jul 27 15:14:40 AEST 2006
Say I have an enum something like the following.
:- type my_enum --->
value1 ;
value2 ;
value3.
If I am working with such a type in foreign code there are a couple of
ways I could do it.
1. Assume that value1, value2 and value3 map to the values 0, 1 and 2,
and then just test against or assign these values. Efficient, but may
be broken by change in representation.
2. Export Mercury predicates that work on this type, and use these
exported predicate exclusively.
For example:
:- pragma export(my_enum_is_value1(in), "module_my_enum_is_value1").
:- pred my_enum_is_value1(my_enum::in) is semidet.
my_enum_is_value1(value1).
:- pragma export(my_enum_is_value2(in), "module_my_enum_is_value2").
:- pred my_enum_is_value2(my_enum::in) is semidet.
my_enum_is_value2(value2).
:- pragma export(my_enum_is_value3(in), "module_my_enum_is_value3").
:- pred my_enum_is_value3(my_enum::in) is semidet.
my_enum_is_value3(value3).
:- pragma export(my_enum_value1 = out, "module_my_enum_value1").
:- func my_enum_value1 = my_enum.
my_enum_value1 = value1.
:- pragma export(my_enum_value2 = out, "module_my_enum_value2").
:- func my_enum_value2 = my_enum.
my_enum_value2 = value2.
:- pragma export(my_enum_value3 = out, "module_my_enum_value3").
:- func my_enum_value3 = my_enum.
my_enum_value3 = value3.
This second way will not break if the representation changes, but is
very cumbersome to use. Interestingly, mtcltk uses both of these
methods when dealing with tcl_status - when writing to a variable it
uses method one, but when reading from it it uses method two.
Which of these methods is likely to be better?
Jon
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to: mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions: mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the users
mailing list