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

Soft scope vs hard scope #1031

Open
banx opened this issue Mar 26, 2021 · 6 comments
Open

Soft scope vs hard scope #1031

banx opened this issue Mar 26, 2021 · 6 comments
Labels
backend Concerning the julia server and runtime display & PlutoRunner & AbstractPlutoDingetjes.jl expression explorer Figuring out assignments and references in a cell help welcome If you are experienced in this topic - let us know!

Comments

@banx
Copy link

banx commented Mar 26, 2021

Consider the following code block

begin
	h = 0
	s = 2
	for i in 1:10
		y(t) = t^s+i  # comment this line to check the inconsistency.
		h += 1
	end
	h
end

Upon execution, the interpreter spits the following error: UndefVarError: h not defined
Regardless of what is the function, the error persists (e.g. defining y(t) = 0 will results with the same error). Using the short form or with function keyword gives the same error.
Of course, the expected execution in this example should give a result h = 10.

This issue was first raised in julia repo, but we've noticed that the REPL results are consistent. However, in Pluto, the error is present when the function definition is present, but no errors when the function definition is absent.

@fonsp
Copy link
Owner

fonsp commented Mar 26, 2021

Thanks!

For anyone who wants to contribute: This is caused by the different scoping rules (soft vs normal) that Julia uses for toplevel code vs inside functions. I don't know exactly how this works, but we should match the REPL behavior.

Pluto wraps code in functions (#720) if the code is 'simple'. Defining a function is one of the triggers to disable function wrapping, explaining the difference. (Just like macro calls, writing @__FILE__ instead of the function definition should give the same result.)

@fonsp fonsp added backend Concerning the julia server and runtime display & PlutoRunner & AbstractPlutoDingetjes.jl help welcome If you are experienced in this topic - let us know! labels Mar 26, 2021
@banx
Copy link
Author

banx commented Apr 2, 2021

I can't think of a way to fix this without keeping the same results as the REPL. I understand that wrapping code inside a function will benefit some speedup, but at the cost of inconsistencies. IMHO, I think keeping the code unwrapped will match the REPL results, as well as the performance of the REPL.

@fonsp
Copy link
Owner

fonsp commented Apr 2, 2021

I disagree with your suggestion.

For anyone who wants to contribute, don't be discouraged, and take a look at: https://github.com/stevengj/SoftGlobalScope.jl

@banx
Copy link
Author

banx commented Apr 2, 2021

Thanks for pointing out to this package, I wasn't aware of its existence. I'll try to see how it can be used to resolve the issue in this context.

@fonsp fonsp added the expression explorer Figuring out assignments and references in a cell label Apr 13, 2021
@fonsp fonsp changed the title Outer variable shadowed when defining a function inside a for loop Soft scope vs hard scope May 2, 2021
@hofmannmartin
Copy link

From a slack discussion that took place yesterday I can contribute the following example, where a simple comment seems to switch the behavior of a cell from non-interactive to interactive context as shown in this notebook.

Screenshot from 2022-05-02 10-28-07

I would expect consistent behavior. @fonsp Which one is the indented one?

@fonsp
Copy link
Owner

fonsp commented May 10, 2022

I think the switch happens because the line Ntriangles1 += 1 references a global that is not (yet) defined, which will error. I believe that this error means that #720 is not applied to the first cell, which can change the behaviour.

Can you find an example of a cell that should work, but it gives an error/warning? You could define a struct inside the cell to disable #720 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Concerning the julia server and runtime display & PlutoRunner & AbstractPlutoDingetjes.jl expression explorer Figuring out assignments and references in a cell help welcome If you are experienced in this topic - let us know!
Projects
None yet
Development

No branches or pull requests

3 participants