Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia macros: @__FILE__ @__LINE__ in macro

Tags:

macros

julia

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 make it print fl.jl:4 | m?

like image 226
Rolf Wester Avatar asked Jun 09 '17 08:06

Rolf Wester


2 Answers

The following will work in the current Julia nightly:

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 by the following implementation pull request. It is not possible in any officially released version, unfortunately.

like image 118
Isaiah Norton Avatar answered Sep 30 '22 13:09

Isaiah Norton


Though there may be more elegant ways to do this, if you don't 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__))
like image 39
houtanb Avatar answered Sep 30 '22 12:09

houtanb