Hey folks!
If I write @btime, @be, @testset, @test, etc. in the REPL, I don’t want to see
ERROR: LoadError: UndefVarError: `@testset` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
in expression starting at REPL[2]:1
If BenchmarkTools.jl / Chairmarks.jl / Test.jl / etc. are dev tools you use, then when you invoke them, you may, like me, want them to run rather than prompt for missing imports. To streamline my workflow slightly, I include the following in my ~/.julia/config/startup.jl file:
if isinteractive()
import BasicAutoloads
BasicAutoloads.register_autoloads([
["@b", "@be"] => :(using Chairmarks),
["@benchmark"] => :(using BenchmarkTools),
["@test", "@testset", "@test_broken", "@test_deprecated", "@test_logs",
"@test_nowarn", "@test_skip", "@test_throws", "@test_warn", "@inferred"] =>
:(using Test),
["@about"] => :(using About; macro about(x) Expr(:call, About.about, x) end),
])
end
And add BasicAutoloads.jl, Chairmarks.jl, BenchmarkTools.jl, and About.jl to my default environment. This means that whenever I run one of the commands listed above for the first time in a REPL session, the appropriate package is automatically loaded.
More generally, BasicAutoloads.jl lets you run an arbitrary Julia expression when you type a symbol into the REPL for the first time. If you attempt to auto-load a package that you don’t have in any environment on your load path, you will get the interactive prompt to install it.
Inspired by this Zulip thread, @fredrikekre’s startup.jl, discussion with @tecosaur, and feedback+advice about the interactive prompt from @aplavin