Next: , Previous: , Up: Foreign language interface   [Contents]


14.6 Using foreign enumerations in Mercury code

The values used to represent a Mercury enumeration type in a foreign language may be explicitly assigned by the programmer using a declaration of the form:

:- pragma foreign_enum("Lang", MercuryType, CtorValues).

CtorValues is a list of pairs of the form:

[
    ctor_0 - "ForeignValue_0",
    ctor_1 - "ForeignValue_1",
    ...
    ctor_N - "ForeignValue_N"
]

The first element of each pair is a constructor of the type MercuryType, and the second is a value in the language Lang that will be used to represent that constructor. The mapping defined by this list of pairs must form a bijection.

Mercury implementations may impose further foreign language-specific restrictions on the form that values used to represent enumeration constructors may take. See the language specific information below for details.

The Mercury implementation is not required to check the validity of foreign enumeration values.

A ‘pragma foreign_enum’ declaration must occur in the implementation section of the module that defines the type MercuryType. It is an error if the type MercuryType is the subject of more than one ‘pragma foreign_enum’ declaration for a given foreign language.

Note that the default comparison for types that are the subject of a ‘pragma foreign_enum’ declaration will be defined by the foreign values, rather than the order of the constructors in the type declaration (as is usually the case).