[MLton-user] FFI type error

jq psychoutstuff@yahoo.com
Thu, 3 Aug 2006 20:12:34 -0700 (PDT)


I want to interface SML with a C function that returns arrays. A problem arises when I try to
use the imported function with List.map.

test.c:
#include <malloc.h>
int*cfoo(int n) { return NULL; }


test.sml:
val foo = _import "cfoo" : int -> int array;
val _ = List.map foo [3]

The List.map line causes this error during compilation:
Type error: bad primapp
cfoo(word32)
Type error: analyze raised exception unhandled exception: TypeError

unhandled exception: TypeError


The following changes avoid the type error, but aren't always viable workarounds.

Explicitly build the list instead of using List.map:
val _ = [foo 3]

Use an SML function instead of importing a foreign function:
fun foo n = Array.array (2, n)

Import a function that doesn't return arrays:
val foo = _import "cfoo" : int -> int;


I'm using MLton built from SVN around 20060801 on i386. I compile with:
gcc -c test.c -o test.o
mlton -default-ann 'allowFFI true' -output test test.sml test.o

Any suggestions for getting this example to compile?

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com