Next: , Previous: Using pragma foreign_export_enum for Java, Up: Interfacing with Java   [Contents]


15.10.3.4 Using pragma foreign_proc for Java

The Java code from Java ‘pragma foreign_proc’ declarations will be placed in the bodies of static member functions of an automatically generated Java class. Since such Java code will become part of a static member function, it must not refer to the this keyword. It may however refer to static member variables or static member functions declared with ‘pragma foreign_code’.

The input and output variables for a Java ‘pragma foreign_proc’ will have Java types corresponding to their Mercury types. The exact rules for mapping Mercury types to Java types are described in Java data passing conventions.

The Java code in a pragma foreign_proc declaration for a procedure whose determinism indicates that it can fail must assign a value of type boolean to the variable SUCCESS_INDICATOR. For example:

:- pred string.contains_char(string, character).
:- mode string.contains_char(in, in) is semidet.

:- pragma foreign_proc("Java",
    string.contains_char(Str::in, Ch::in),
    [will_not_call_mercury, promise_pure],
"
    SUCCESS_INDICATOR = (Str.IndexOf(Ch) != -1);
").

Java code for procedures whose determinism indicates that they cannot fail should not refer to the SUCCESS_INDICATOR variable.

Arguments whose mode is input will have their values set by the Mercury implementation on entry to the Java code. With our current implementation, the Java code must set the values of all output variables, even if the procedure fails (i.e. sets the SUCCESS_INDICATOR variable to false).