[MLton] Callback to function pointer?

Stephen Weeks MLton@mlton.org
Tue, 12 Jul 2005 21:52:03 -0700


> Is something like the following supposed to be legal?:
> 	fun f (x: 'a) =
> 	       let fun thunk () = x
> 	       in (_export "zulu": unit -> 'a) thunk
> 	       end
> (It is rejected.) 

It is rejected because it doesn't make sense for the FFI to deal with
polymorphism.

> If it were expected, would there be one cell total for zulu's?  One
> per type?

One cell total.

> Given the flat and global nature of the export name space, wouldn't it be
> better if the type of
> 	_export "<C name>": 'a
> was a 'a ref?  (Or perhaps a 'a option ref, initially NONE/NULL?)  

That does seem logical, but there's still some weirdness anyway in
that the ref is created once for the whole program rather than once
per evaluation of the _export expression.

> Then the assignment nature would be clear and the semantics would be
> that the value of the _export ... was to look up the global ref cell
> associated to the name and to return it.

Having the _export be of type ('a -> unit) equally well conveys that
something is being side effected.  Further, abstracting away from the
ref cell by only exposing the setter gives us the flexibility to do
more, like report an error if the setter is called twice (not that we
do this, but we could).  It also lets us choose a different
representation, which we do take advantage of -- there is no 'a ref,
in fact, there is a (unit -> unit) array.