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

The README.md is unclear about imports #115

Open
VincentPikand opened this issue Jun 13, 2023 · 2 comments
Open

The README.md is unclear about imports #115

VincentPikand opened this issue Jun 13, 2023 · 2 comments

Comments

@VincentPikand
Copy link

VincentPikand commented Jun 13, 2023

This part of the README

Screenshot 2023-06-13 at 11 42 23

My problem

I don't understand how using is more explicit and clearer than import.

Consider this example:
Imagine this code is part of some bigger library and somebody forgot to add the prefix LinearAlgebra to eigvals, which I believe is the intended style because then the namespace is clear.

julia> import LinearAlgebra
       x = [1 0 0; 0 1 0; 0 0 1]
       print(eigvals(x))
ERROR: UndefVarError: eigvals not defined

We get a nice error, because eigvals is not in our namespace. However, this code is not following the Blue style guide because of

Prefer the use of using over import to ensure that extension of a function is always explicit and on purpose

Writing this using the blue style guide we don't get an error, so mistakes like this can easily go unnoticed.

julia> using LinearAlgebra
       x = [1 0 0; 0 1 0; 0 0 1]
       print(eigvals(x))
[1.0, 1.0, 1.0]

also,

Note: Prefer the use of imports with explicit declarations when writing packages.

what imports, imports as in import or imports in general? An example would make it clearer.

@omus
Copy link
Contributor

omus commented Jun 13, 2023

That particular example you show is specifically talking about avoiding using import to add methods to functions defined by other packages.

For usage of code using import LinearAlgebra is reasonable but personally I'd probably end up doing using LinearAlgebra: LinearAlgebra for the same effect.

@VincentPikand
Copy link
Author

That particular example you show is specifically talking about avoiding using import to add methods to functions defined by other packages.

Yeah, I realized that just now, I was tunnel visioning on the syntactic differences between import and using. Thank you for the clarification.

However, I still feel like the advice on import vs using could be a bit more nuanced — it would make sense to me if there was a line or two about when to prefer import, because from a more traditional software engineering perspective (compared to scientific computing) it has it's advantages.

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

2 participants