[MLton] bool and MLton FFI

Wesley W. Terpstra wesley@terpstra.ca
Fri, 23 Jun 2006 14:27:45 +0200


On Jun 23, 2006, at 4:52 AM, Stephen Weeks wrote:
>   1. No bool in the FFI.
>   2. C Bool in the FFI with manual conversion to/from SML bool.
>   3. Bool in the FFI with automatic conversion between C bool and SML
>      bool.

After more reflection, I think #2 is best.

> I think (2) will be slightly counter-intuitive to
> users, because all the other C types in the FFI have an SML type they
> are equivalent to (even C int is equivalent to some SML int type, even
> though it changes from platform to platform).

I disagree here. We should start encouraging users to use _import  
like this:
	_import "foo" : C_Int.t * C_Long.t * Int32.t-> C_Bool.t
for the function:
	extern bool foo(int x, long y, int32_t z);
Otherwise their code won't be portable.

Requiring them to use the C_Bool type for FFI is then not so unusual.

The user-visible rule is: if it's not a fixed size, use C_X for type  
X. You might need to convert types for int -> C_Int.t, but that's the  
price you pay to interface with C. Plus, the conversion happens in  
SML, with no C wrapper needed, and an overflow exception if you mess up.

As a tangental aside, it also makes C_Bool.t vector work insofar as  
Vector.map C_Bool.fromBool v works.