-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
module and import aliasing #1255
Comments
I realized minutes ago that you can do the following:
I still think it would be nice to have the "import as" notation as syntactic sugar though. |
While I'm here and thinking about it, it would also be nice to have a way of importing multiple specific functions in the same import line, as mentioned in @JeffreySarnoff 's forum post. A search through the issues didn't reveal such a request yet. Something like # from the forum post
import Module.(export1,export2,export3)
# or
import Module.[export1,export2,export3]
# or maybe
import Module.{export1,export2,export3} It's a different issue than this one, but related. Should I
(I'm assuming the usefulness of such a feature is not controversial...) Edit: This works now with |
I think that it should also support e.g.
to be able to rename module members upon import. |
Wondering how renamed function names would participate in dispatch? |
It would have to work basically like an alias, equivalent to |
import Base.bar as foo we can definitely use foo as Base.bar, but would defining foo also override |
This is an old issue, but I think it is time to revisit this as we are approaching 0.2 In writing codes, I found I always wanted to write I guess this is not too difficult to implement, and in my opinion, this is much nicer than writing import NumericExtensions
const ne = NumericExtensions |
+1 |
1 similar comment
+1 |
Is this still relevant? I personally dont' mind just doing the extra |
The value of this feature is that you could avoid importing the original name. |
+1 |
+1 |
Rather than introducing a new keyword import Tlahuixcalpantecuhtli => Venus: xxxxxxxxx => x9, yyyyyyyy => y8, zzz This would orthogonally allow aliasing of the module and any subset of variables in the same syntax. |
I don't know, that seems really ugly to me. |
import Tlahuixcalpantecuhtli as Venus: xxxxxxxxx as x9, yyyyyyyy as y8, zzz Is that any better? I find it far less readable. |
I personally find full disclosure: python bias. |
I like the |
I like bikesheds that get stuck in Nash equilibria. |
Why would you rebind the inner symbols of a module? Its ugly because you really should not be doing that. I like Haskell's import syntax. I feel Python's namespaces are more granular than Julia's modules. Julia's module's feel more in spirit to haskell's modules, so maybe we should look there for inspiration. |
I agree with @ssfrr , the from Tlahuixcalpantecuhtli as Venus import xxxxxxxxx as x9, yyyyyyyy as y8, zzz I feel like I'm talking to the REPL. But I know that this kind of suggestion is not feasible. |
This is one of my least favorite syntactic choices in Python. To change the meaning slightly you have to rearrange the entire line radically, changing the initial keyword and the ordering of everything. It looks completely different from other imports, obscuring that fact they that do almost the same thing. This is a terrible syntactic design, imo, giving far too much weight to being superficially English-like. |
👍 for
|
I never heard of Nash equilibria and after reading about it I don't think this constitutes one. But the funny thing is, while this bikeshed has 10 responses in one hour, a proposal on #6984 which strikes me as very important (but more difficult) has gone 4 hours with no comment! As for |
Good point @BobPortmann on #6984. I think |
I didn’t intend for my message to imply that this isn’t a big deal. I would still prefer a syntax for this. |
I agree. Also, for: using ImportMacros
@using OtherPackage as ... It makes it a bit annoying when presenting the language to newcomers or programming beginners to explain why a given script starts with this inconsistency, as it complicates the flow and shifts the focus of the teaching.
The reason why people don't use ImportMacros a lot in actual code a lot could be due to a tradeoff. For instance, I'd be using So yeah, "ImportMacros is sufficient". For people that really want to use it. But that's the thing, it's not a vital issue, and people can work without it. Nonetheless, I strongly believe it is typically these little things that make for a language's syntax to stick, hence this monster-thread to add its support to base. I think that And the fact that Python had it implemented first shouldn't really be relevant; Julia should strive at having the best syntax before trying to distance itself from other languages and/or emphasize its own identity. |
Having syntax for this and conventions around popular modules (I'm thinking of how it's always |
I don't understand why you think it is an inconsistency, it is simply namespace management, which is a normal part of programming in the large. Also, renaming modules may not be something that newcomers to Julia would encounter first. I am not saying that there isn't a use case, but it may be to rare to justify its own keyword. At the moment, searching for
This cuts both ways: if the (very minor) cost of using a ligthweight package like
I am not sure why you think this is a motivation in this discussion. |
There are some cases where you might want to use julia> using Distributed
julia> import Future; const future = Future
Future
julia> Future === Distributed.Future # oops, this is what we wanted
false
julia> Future === future # not what we wanted `Future` to mean
true Yes, you can work around this, but it's awkward. Given that it's often handy, sometimes needed and that |
New Julia user here. I don't know how complex adding this syntax is? Is it doable for a newbie? |
Probably not very easy. It will involve hacking the parser code which is written in Scheme and wiring the change through the AST, potentially touching some C code as well. |
It would be a nice feature for avoid function name conflict in multiple modules. |
From a discussion on slack, I add some thoughts I wonder if this will make people less careful with choosing function names and properly adding methods to the correct functions. The fact that, e.g.,
are different functions is a surefire way of having to implement two version of your function if you want it to work for both numerical and symbolic inputs. If such namespacing becomes widespread in julia as well due to the convenience, we might miss out on a lot of code reuse? I.e., I'm afraid this might give us less of "This encourages people to work together" as it was put by Lyndon in https://www.oxinabox.net/2020/02/09/whycompositionaljulia.html |
@baggepinnen I don't think this changes anything in that regard. In Julia methods implemented in different modules are not merged by importing both methods into the same namespace. julia> module Foo
export sin
sin(s::String) = uppercase(s)
end
Main.Foo
julia> sin(1)
0.8414709848078965
julia> using .Foo
WARNING: using Foo.sin in module Main conflicts with an existing identifier.
julia> sin("wat")
ERROR: MethodError: no method matching sin(::String)
Closest candidates are:
sin(::BigFloat) at mpfr.jl:727
sin(::Missing) at math.jl:1197
sin(::Complex{Float16}) at math.jl:1145
...
Stacktrace:
[1] top-level scope at REPL[4]:1
[2] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia-1.5.1/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288
julia> Foo.sin("wat")
"WAT" One has to explicitly add a dispatch to the original function for this to work: julia> Base.sin(s::String) = Foo.sin(s)
julia> sin("wat")
"WAT" As far as I can tell, there is no possible way for module aliasing to change how functions are dispatched. And honestly, some functions with the same name should live in separate namespaces. A |
@ninjaaron I might have failed to convey some nuance in my concern above. I am not concerned with this change changing anything other than the strategy people choose to take when they implement libraries. julia> using .Foo
WARNING: using Foo.sin in module Main conflicts with an existing identifier. can choose to either simply |
@baggepinnen That makes sense, and there's no harm in bringing it up. I don't think this will make a huge difference for people creating libraries, since module aliasing will only affect library users. I guess it is possible that having easier module aliasing will result in fewer users complaining about naming conflicts, but I would be surprised if that has a tremendous impact on APIs. When I write a library, I'm usually pretty conscious of whether I want to add a dispatch to a function in Base or I want to keep it separate. I would assume most other library authors are as well. |
@ninjaaron I think if the current convention is using specific implementation for dispatch like
using |
For the module system, we can import and use a module with dot notation:
This can be useful to prevent namespace pollution. Because of the verbosity of module names, however, it would be nice to have module aliases:
The text was updated successfully, but these errors were encountered: