[MLton-user] FFI and finalizable values
Vesa A Norrman
Tue, 16 Dec 2003 16:10:12 +0200 (EET)
> I think that it's easy enough to do in the language by writing a
> wrapper around c_function at the time it is defined, so that you only
> have to use the awkward syntax once per _import.
I agree the change in code size occurs once per each import.
> val c_function = _import "c_function" : Pointer.t -> unit;
> val c_function: Pointer.t Finalizable.t -> unit =
> fn x => withValue (x, c_function)
This is about what I do.
> You can also add helper functions for the more complex cases
> val withValue2: 'a Finalizable.t * 'b Finalizable.t * ('a * 'b -> 'c) -> 'c =
> fn (x, y, f) => withValue (x, fn x => withValue (y, fn y => f (x, y)))
> Or, we might add these to MLton.Finalizable. In any case, with these,
> it is easy enough to define a wrapper for your other case.
> val c_function = _import "c_function" : Pointer.t * Pointer.t -> unit;
> val c_function: Pointer.t Finalizable.t * Pointer.t Finalizable.t -> unit =
> fn (x, y) => withValue2 (x, y, c_function)
In my program nested use of withValue occurs in a situation where
types are coming behind :>. In that case each type needs its own
withValue, therefore I would not find withValue2 usefull.
I was also looking to eliminate a function giving
access to withValue, since FFI can look through :>.
> Hopefully that works well enough, especially since it's all done once
> per _import.
FFI sure works well enough. I was trying to simplify...