Hi, I am not certain this is the appropriate forum for this - or if I should just be making a PR. I’m not part of the Julia dev team (don’t know the procedure).
I realized it might be nice to have two small macros added to Julia’s library:
- Something to “expand” source-relative paths.
- Something to quickly disable a multi-line expression
Source-relative paths:
Mostly copied from @__DIR__
:
macro srelpath(filepath)
__source__.file === nothing &&
return throw("Cannot find source-relative path")
_dirname = dirname(String(__source__.file))
_dirname = isempty(_dirname) ? pwd() : abspath(_dirname)
return joinpath(_dirname, filepath)
end
This would allow one to more easily pass a source-relative (instead of cwd-relative) path to functions in other modules, for example:
data = readdlm(@srelpath("delim_file.txt"), '\t', Int, '\n')
It is basically just a shorthand (following other *path()
functions) for the following:
data = readdlm(joinpath(@__DIR__, "delim_file.txt"), '\t', Int, '\n')
It is a small change, but I think it slightly improves readability.
Quick expression-level diable:
macro X(ex); return nothing; end
Very simple, but it would allow one to quicly disable multi-line expressions such as a function definition or large data declarations (alternative to #= [...] =#
):
@X function MyFun()
println("Running...")
end
@X b = Dict(
:a => 54,
:b => 7,
:c => 11,
:d => 22,
)
Again, it is only a small change from encasing with #= [...] =#
, but I think it is (sufficiently) often easier to disable/enable an expression by prepending something instead of encasing it with #= [...] =#
.