Skip to content

Instantly share code, notes, and snippets.

@yonta
Last active August 29, 2015 14:14
Show Gist options
  • Save yonta/6a120f5632e0b3783e18 to your computer and use it in GitHub Desktop.
Save yonta/6a120f5632e0b3783e18 to your computer and use it in GitHub Desktop.
SML# test
gcc -o hoge.o -c hoge.c
smlsharp -o run fuga.sml hoge.o
./run
(* できない
(type inference 004) not an interoperable type: int
ref
*)
val f1 = _import "hoge" : (int ref -> int) -> ()
val () = f1 (op !)
(* できる、けどptr受け取る関数がSML#では作れないから半分死んでるんじゃ *)
(* いや、builtin関数使えばいけるか? *)
val f2 = _import "hoge" : (int ptr -> int) -> ()
val () = f2 (SMLSharp_Builtin.Pointer.deref)
(* できる *)
val f3 = _import "hoge2" : (int ref) -> ()
val () = f3 (ref 2)
(* できない、公式にptr型相当の返り値を禁止している
(type inference 004) not an interoperable type: int
ref
*)
val f4 = _import "hoge3" : (int) -> int ref
val () = (print o Int.toString o ! o f4) 0
(* できる *)
val f5 = _import "hoge4" : (int -> int ref) -> ()
val () = f5 (fn x => ref x)
void hoge(void (*f)(int*))
{
return;
}
#include <stdio.h>
void hoge2 (int* n)
{
printf("%d\n", *n);
}
int* hoge3(int n)
{
int* p = &n;
return p;
}
void hoge4(int* (*f)(int))
{
printf("%d\n", *(f(0)));
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment