Re: Getting the current line number
Geoffrey Alan Washburn <[email protected]>
| Newsgroups | gmane.comp.lang.sml.smlnj |
|---|---|
| Message-ID | <[email protected]> |
John Reppy wrote:
> After thinking about it a bit more, I realized that the following hack
> would work. First, define the following helper function
>
> fun file_line ex = let
> val [s] = SMLofNJ.exnHistory ex
> val (f, r) = Substring.splitl (fn #":" => false | _ => true)
> (Substring.full s)
> val ln = Substring.takel (fn #"." => false | _ => true)
> (Substring.triml 1 r)
> in
> {file = Substring.string f, line = Substring.string ln}
> end;
>
> then you can get the file and line info at a given source code location
> by embedding the following code:
>
> file_line ((raise Fail "") handle ex => ex);
It just occurred to me, for the future reference of others, that you
could probably actually make this is a little lighter as
exception Info
fun file_info (exthunk : unit -> string list) = let
val [s] = (exthunk () handle ex => SMLofNJ.exnHistory ex)
val (f, r) = Substring.splitl (fn #":" => false | _ => true)
(Substring.full s)
val ln = Substring.takel (fn #"." => false | _ => true)
(Substring.triml 1 r)
in
{file = Substring.string f, line = Substring.string ln}
end
val a = file_info (fn _ => raise Info)
val b = file_info (fn _ => raise Info)
Which actually isn't too bad, especially if body of the function using
the information is a mostly a let expression already. I don't see any
way to do better without a preprocessor, an extension to SML/NJ, or
another esoteric SML/NJ internals feature.
-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl