Is there an easy way to change a "for loop header" depending on the packages a user has? For example, @progress for is good for adding a progress bar in Juno/Atom (just found out!), while we also have things like @simd, @acc, and @parallel. So what I want to have on this loop is to put a bunch of these macros conditionally given a boolean from the user or depending on availability. However, if I do a simple if isdefined(@progress) @progress for ... elseif accelerate @acc for ... elseif @parallel for ... end
or something of that sort, I would have to keep pasting around the same for loop code. Is there some more elegant way of doing this? Also, I may want to combine some, and so once you start looking at the viable combinations that ends up being a lot of code!
The Pkg.installed
method will error if the package isn't installed. It takes a string, and returning the decorated expression after that line with the other possibility in the catch block is effective for this sort of thing:
macro optional_something(pkg, expr)
try
Pkg.installed(string(pkg)) == nothing && return expr
esc(quote
@time $expr
end)
catch
expr
end
end
# this won't add the macro @time
@optional_something XXX rand(1000)
# this will
@optional_something Plots rand(1000)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.