Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@require-else #41

Open
oxinabox opened this issue Jan 16, 2018 · 4 comments
Open

@require-else #41

oxinabox opened this issue Jan 16, 2018 · 4 comments

Comments

@oxinabox
Copy link

Could we have a kind of If else block?

I'm thinking I would like to @require HTTP.jl,
but if that hasn't been loaded,
then fallback to defining my methods in terms of called to run(`curl ...`)

So that would look something like (and I am making up syntax)

@require if HTTP
     get_headers(url) = HTTP.head(url).headers
else
     get_headers(url) = readstring(`curl -sI -L $url)')
end

This task actually takes something way more complex but I am sure you see the point

@MikeInnes
Copy link
Collaborator

This doesn't really work in Requires' model, since HTTP could be loaded at any time. So it'd have be have semantics more like before/after, which you can do already:

get_header(url) = readstring(...)
@require HTTP get_header(url) = HTTP...

This will get you an ugly method definition warning though; it's probably better to just to

get_headers(url) = isdefined(Main, :HTTP) ? ...

@oxinabox
Copy link
Author

Performace-wise that would be sub-optimal, but it is probably negligible anyway, since it is no fast operation to begin with.

Just checking main probably isn't enough since my package could be being used from within another module which has loaded HTTP.jl (because that package might know that they need HTTP's extra robustness)

(cf: oxinabox/DataDeps.jl#23)

@MikeInnes
Copy link
Collaborator

Checking Main is enough on Julia 0.6 as all modules get loaded into main, if they are loaded at all.

Not sure how this changes on 0.7 though.

@aminya
Copy link

aminya commented Aug 18, 2019

This is very useful! For example, I can use two different methods for a function depending on the @requires output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants