[MLton] SML vs. the C preprocessor

Wesley W. Terpstra terpstra at dvs1.informatik.tu-darmstadt.de
Tue Jul 17 04:35:39 PDT 2007


One idiom I used to use frequently in C++ for debugging was to wrap a  
function in a macro that recorded call-site information when compiled  
in debug mode. For example:

#ifdef DEBUG
#define foo(x, y) real_foo(__FILE__, __LINE__, x, y)
void real_foo(const char* caller_file, int caller_line, int x, int y) {
#else
void foo(int x, int y) {
#endif

Obviously you wouldn't do this for all (or even most) functions, but  
in some cases it's VERY helpful. In particular, when doing async  
programming, sometimes you'd like to be able to phrase the question:  
what event handlers are still pending and what code created them?  
This helps track down 'event leaks' where you are waiting for  
something to happen that never will.

I know that there's no way to do this with SML. I'm not even sure how  
a MLton specific extension would look. In the example, it's not  
enough to just provide __FILE__ and __LINE__. The macro hack made it  
possible to grab this information from the caller, without the caller  
explicitly providing it. C's 'assert' method does essentially the  
same thing.

Do other people miss __FILE__ and __LINE__? Does anyone see a way of  
getting similar functionality?




More information about the MLton mailing list