[MLton] _pos aka __FILE__ __LINE__ etc

Vesa Karvonen vesa.a.j.k at gmail.com
Sat Jul 11 04:44:11 PDT 2009


On Wed, Jul 8, 2009 at 5:02 PM, Wesley W. Terpstra<wesley at terpstra.ca> wrote:
> Would anyone else like a _pos language extension that evaluates to the
> current file, line, and column? For use in something like: assert (_pos,
> "Bad mojo", x < j)
>
> It could return "foo.sml:4.2" or maybe ("foo.sml", 4, 2) or even
> ("/home/terpstra/BubbleStorm/demo/foo.sml", 4, 2).

Not that I wouldn't like to have a robust method of implementing
assert that reports a useful source location like this, but there are
some issues.  First of all, having to explicitly pass _pos to assert
is cumbersome.  Second, the _pos token would not be portable.  The
code would simply not compile with other ML compilers that do not
support the extension.

With the current MLton, you can get a stack trace (exception history)
by calling MLton.Exn.history.  You could even use the exception
history to implement an assert function that reports an approximate
caller source location.  However, I think that it is usually more
helpful to see the whole trace.  Perhaps the main problem with using
exception history is that the current implementation of
MLton.Exn.history is rather costly and can be prohibitive.

-Vesa Karvonen



More information about the MLton mailing list