fonsp / Pluto.jl

🎈 Simple reactive notebooks for Julia
https://plutojl.org/
MIT License
4.95k stars 286 forks source link

Can't use macro from `MyModule` in the cell that calls `using MyModule` #1592

Closed greimel closed 2 years ago

greimel commented 2 years ago

https://user-images.githubusercontent.com/6280307/138504368-8e936962-0962-421a-8b07-ccaacacf353d.mov

# ╔═╡ 6914a6fb-085c-451b-bcd9-b42a9ae0f2f7
nt = (x = 1, y = 2)

# ╔═╡ 1803b65a-3362-11ec-3aa0-51d6c9b40851
begin
    using UnPack

    @unpack x = nt
end

# ╔═╡ 0bd5f483-8401-472c-b47d-dbd2b55ee68e
@unpack y = nt

# ╔═╡ 7eaa8cbd-a20b-4012-a12d-a072441f1ecf
x

# ╔═╡ 8556f8c2-b3cc-4542-a3ce-1bb956d43501
y
greimel commented 2 years ago

I assume that's @Pangoraw 's business :-)

Pangoraw commented 2 years ago

This is because you cannot use a macro in the same expression where you define it because the macro expansion happens before the expression is evaluated. The same is true in the REPL:

julia> begin
           using DrWatson
           @quickactivate
       end
ERROR: LoadError: UndefVarError: @quickactivate not defined
in expression starting at REPL[1]:3

In version 0.16.1 the execution model would pre-run all usings statements so that's why it was possible. This was removed because it caused other unexpected behaviors like #1511. This is likely to stay that way now, because Pluto notebooks can also be run as plain julia scripts.