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

Could using X as Y be syntactic sugar for using X; const global Y = X #38521

Open
DilumAluthge opened this issue Nov 21, 2020 · 9 comments
Open

Comments

@DilumAluthge
Copy link
Member

DilumAluthge commented Nov 21, 2020

The syntax using X as Y currently throws a syntax error:

julia> using DataFrames as df
ERROR: syntax: invalid syntax "using DataFrames as ..."
Stacktrace:
 [1] top-level scope
   @ none:1

Would it be possible for using DataFrames as df to be syntactic sugar for this:

using DataFrames
const global DataFrames = df
@DilumAluthge
Copy link
Member Author

@JeffBezanson (since you authored #37396) and @fredrikekre (since you wrote ImportMacros.jl)

Any thoughts?

@DilumAluthge DilumAluthge changed the title Could using X as Y expand to using X; const Y = X Could using X as Y expand to using X; const global Y = X Nov 21, 2020
@DilumAluthge
Copy link
Member Author

DilumAluthge commented Nov 21, 2020

This is basically what ImportMacros.jl does right now when you do this:

using ImportMacros

@using DataFrames as df

But since the ImportMacros.jl package has been deprecated, I figured it'd be nice to have this syntactic sugar in Julia.

@DilumAluthge DilumAluthge changed the title Could using X as Y expand to using X; const global Y = X Could using X as Y be syntactic sugar for using X; const global Y = X Nov 21, 2020
@jebej
Copy link
Contributor

jebej commented Nov 21, 2020

If you need a qualifier (df), it seems more consistent to just import DataFrames as df and use df.fun everywhere.

@stevengj
Copy link
Member

See also #1255

@fredrikekre
Copy link
Member

fredrikekre commented Nov 22, 2020

This was discussed and decided against in #37396, no? #37396 (comment) and the two following comments.

@StefanKarpinski
Copy link
Sponsor Member

StefanKarpinski commented Nov 24, 2020

The meaning that would seems to make sense for using DataFrames as DF would be to make it the same as using DataFrames but importing the name DataFrames itself as DF.

@DilumAluthge
Copy link
Member Author

The meaning that would seems to make sense for using DataFrames as DF would be to make it the same as using DataFrames but importing the name DataFrames as DF.

Yeah, that's exactly what I had in mind.

@nickrobinson251
Copy link
Contributor

nickrobinson251 commented Nov 30, 2020

This was the first thing i tried when i read about the ... as ... syntax in Julia v1.6 (using PowerSystems as PSY) and was surprised it didn't work (as short-hand for using PowerSystems; const PSY = PowerSystems, but without the PowerSystems name also in-scope).

Is there some other behaviour that could have been expected such that using X as Y is ambiguous in some way?

@oxinabox
Copy link
Contributor

For this to be really useful It can't literally be syntactic sugar for using X; const global Y = X
because if X clashes with a variable that is in-scope then you can't load the package in the first place.
import X as Y doesn't have that problem.

Instead it needs to never bring the module's actual name into scope; and create a new name for it

julia> Pipe  # use this so it is incope
Pipe

julia> import Pipe as P2  # no problem

julia> using Pipe
WARNING: using Pipe.Pipe in module Main conflicts with an existing identifier.
ERROR: importing Pipe into Main conflicts with an existing identifier

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

7 participants