Robert Roessler wrote:
David Matthews wrote:
I thought that it would be quicker and easier to implement this myself rather than try to explain what to do. Try it out and let me know if it does what you want. I've extended the example in the mlsource/extra/CInterface/Examples directory to include a test.
Thanks, David - this certainly does what I need (and more) - and you get to spell finalize your way! :)
On the ML side, I only have one question: why did you change the typing of your setFinal from that in my set_finalize? Besides the fact that the curried form appears to be the "style" of CInterface, it just looks more pleasing - and it reminds one that this *isn't* C/C++. ;)
Who can look at
val setFinal : vol * sym -> unit
and think it looks cooler than
val setFinal : vol -> sym -> unit
??? And of course, the lost opportunities for partial application (whether that is useful in this specific instance or not)...
Personally, I prefer not to curry functions if they require all their arguments to work and there's no obvious requirement for partial application, but that's very much a matter of personal taste. Actually, as I think about it this could usefully be curried if its type was val setFinal: sym -> vol -> unit It's quite likely that the same finalisation function would be used on different vols whereas I can't see why one would apply a different finaliser to the same vol.
On the foreign.cpp side of things, I notice 3 differences vs my version:
- I had it so that "finalisation" was only possible with NON-owned
vols, while your approach potentially allows Poly to use this feature now also.
Well, the vol that has the finaliser actually is an owned vol. That's because the C_pointer field contains the address of a malloc'd area that holds the C value. So when a C function returns a pointer value a new "vol" is allocated to contain the returned value. This extra level of indirection means that a vol can be a char, an int or a struct.
- My field ordering was different in Volatile - since pointers can be
larger than ints, I slipped this in BEFORE the
Bool Own_C_space;
I prefer to keep structs "packed" for alignment and aesthetic reasons.
Actually, this "Bool" is an int (really an unsigned or size_t) that contains the size in bytes of the area. Calling it Bool looks like a piece of legacy code.
- Your form of "return unit" is obviously canonical - I was just thrown
by a couple of instances of
return h; /* to be ignored */
That's actually wrong. (I didn't write the FFI code!) It is theoretically possible for a unit value to be compared for equality with another unit value and if that is done with the fall-back structure equality code it will yield the wrong answer if the same value is not used everywhere to represent unit.
Again, thanks - and if you DON'T have a hardcore reason for using the non-curried form and want me to "fix" setFinal, let me know. :)
I think I prefer it curried but as sym->vol->unit. I'll even accept "finalize" since my dictionary says it's acceptable and perhaps even preferred! It looks like there are a few things that need to be cleaned up anyway so I could make these changes.
Regards, David