@run macro?

How could a run macro be defined that executes a script?

It should just include the file. Would something like:

@run myscript.jl

be possible as replacement for

include("myscript.jl")

?

That would save 7 keystrokes in the REPL.

macro run(script)
    return :(include($script))
end
@run "scripts/tmp.jl"

I don’t see a way of avoiding the quotation marks in the invocation, however.

Function APIs are way more flexible, from my standpoint.

Just like JLD2.@load "a.jld2" can be replaced with first(JLD2.load("a.jld2")).second.

It’s mostly possible if we painstakingly validate an Expr with “variables” and “operators” like /, \, . etc, but it seems like a lot of trouble just to hit a ParseError at a very common C:\. If the point is to save keystrokes, then a single-character alias for include or a nonstandard string literal saves more. To edit your macro, the latter looks like:

julia> macro run_str(script)
         esc( :(include($script)) )
       end

julia> @macroexpand run"C:\blah.jl"
:(include("C:\\blah.jl"))

Note that non-standard string literals are parsed like raw strings (which are actually implemented with no processing) unlike normal Strings, so this doesn’t allow string interpolation or most unescaping.

1 Like

@run my_file.jl wouldn’t TAB-complete in the REPL, which saves far more keystrokes and errors than this macro would IMHO.