Next: , Previous: , Up: Interfacing with C#   [Contents]


14.9.2.4 Using pragma foreign_proc for C#

The C# code from C# pragma foreign_proc declarations will be placed in the bodies of static member functions of an automatically-generated C# class. Since such C# 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 C# ‘pragma foreign_proc’ will have C# types corresponding to their Mercury types. The exact rules for mapping Mercury types to C# types are described in C# data passing conventions.

C# code in a pragma foreign_proc declaration for any procedure whose determinism indicates that it can fail must assign a value of type ‘bool’ to the variable ‘SUCCESS_INDICATOR’. For example:

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

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

C# code for procedures whose determinism indicates that they cannot fail should not access SUCCESS_INDICATOR.

Arguments whose mode is input will have their values set by the Mercury implementation on entry to the C# code. If the procedure succeeds, the C# code must set the values of all output arguments. If the procedure fails, the C# code need only set SUCCESS_INDICATOR to false.