Julia macros: @__FILE__ @__LINE__ in macro

This code:

macro FL(message) 
    return @sprintf("%s:%d | %s", @__FILE__, @__LINE__, message) # line 2
end
println(@FL("m")) # line 4

prints fl.jl:2 | m. How can I print fl.jl:4 | m?

+6
source share
2 answers

In the current Julia, the following will work:

macro FL(message) 
    return :(@sprintf("%s:%d | %s", $(string(__source__.file)), $(__source__.line), $(esc(message)))) # line 2
end
println(@FL("m")) # line 4

This was made possible thanks to the following request for implementation implementation . Unfortunately, in any officially released version this is not possible.

+4
source

Although there may be more elegant ways to do this, if you do not want this to block your progress on other fronts, why not just pass the line number to the macro ...

macro FL(message, line)
    return @sprintf("%s:%d | %s", @__FILE__, line, message)
end
println(@FL("m", @__LINE__))
+3
source

All Articles