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

Handling of err and ans global variables in REPL #43172

Closed
BioTurboNick opened this issue Nov 20, 2021 · 6 comments · Fixed by #48308
Closed

Handling of err and ans global variables in REPL #43172

BioTurboNick opened this issue Nov 20, 2021 · 6 comments · Fixed by #48308
Labels
REPL Julia's REPL (Read Eval Print Loop)

Comments

@BioTurboNick
Copy link
Contributor

Copying this from @mcabbott in merged #40642 to a new issue so it doesn't get lost.

===================
Is it possible that err, and in fact ans, should have a value on startup? This would prevent this:

 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> ans(x) = "function";

julia> 1+1
ERROR: invalid redefinition of constant ans

julia> 2;
ERROR: invalid redefinition of constant ans

in favour of this:

 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> 1+1
2

julia> ans(x) = "function";
ERROR: cannot define function ans; it already has a value

=======================

@vtjnash vtjnash added the REPL Julia's REPL (Read Eval Print Loop) label Nov 20, 2021
@JeffBezanson
Copy link
Member

Yes, I think it would be reasonable to initialize them to nothing.

@StefanKarpinski
Copy link
Member

Doesn't that turn the introduction of err into a breaking change? (Albeit only in the REPL.)

@BioTurboNick
Copy link
Contributor Author

Could an alternative be to set a global inside the REPL module and export it only if not already in Main?

@JeffBezanson
Copy link
Member

We have changed behavior in the repl before, e.g. the scope change. Fortunately may not need to in this case.

Could an alternative be to set a global inside the REPL module and export it only if not already in Main?

Yes I think that works --- if you define it first, you just get your variable and the REPL modifies its own variable which you can't see. But if you request ans or err without defining them, the using logic kicks in to import the variables. Good idea!

@vtjnash
Copy link
Member

vtjnash commented Apr 15, 2022

I made this version of the change, but I don't think I like it master...vtjnash:jn/MainInclude-ans-err

In particular, it means that ans gets hidden from names(Main)

So possibly we want to just declare global ans = nothing; global err = nothing early in REPL startup instead

@JeffBezanson
Copy link
Member

Or the most benign version would be to check whether ans is a constant before trying to set it in the REPL, and just skipping it in that case to avoid the error/warning.

vtjnash added a commit to vtjnash/julia that referenced this issue Jan 16, 2023
This hides them from `names()` unfortunately, but means we will not
accidentally discard a variable from the user in Main, either because
the import will fail or the assignment will fail. If we later have
world-versioned bindings, this would also mean you could toggle (between
worlds) between being module-local and a special import value.

Fix JuliaLang#43172
Fix JuliaLang#48299
vtjnash added a commit to vtjnash/julia that referenced this issue Feb 14, 2023
This hides them from `names()` unfortunately, but means we will not
accidentally discard a variable from the user in Main, either because
the import will fail or the assignment will fail. If we later have
world-versioned bindings, this would also mean you could toggle (between
worlds) between being module-local and a special import value.

This also needed some improvements to docs, so that it would print
docstrings for unassigned globals without also simultaneously claiming
that it did not exist.

Fix JuliaLang#43172
Fix JuliaLang#48299
vtjnash added a commit that referenced this issue Feb 15, 2023
This hides them from `names()` unfortunately, but means we will not
accidentally discard a variable from the user in Main, either because
the import will fail or the assignment will fail. If we later have
world-versioned bindings, this would also mean you could toggle (between
worlds) between being module-local and a special import value.

This also needed some improvements to docs, so that it would print
docstrings for unassigned globals without also simultaneously claiming
that it did not exist.

Fix #43172
Fix #48299
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REPL Julia's REPL (Read Eval Print Loop)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants