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

@SVector & @MVector fail when their inputs use non global scope variables #1014

Closed
dpinol opened this issue Mar 15, 2022 · 2 comments · Fixed by #1009
Closed

@SVector & @MVector fail when their inputs use non global scope variables #1014

dpinol opened this issue Mar 15, 2022 · 2 comments · Fixed by #1009

Comments

@dpinol
Copy link

dpinol commented Mar 15, 2022

This works

       tmp=[1,2]
       @SVector [1 for i in 1:length(tmp)]

but this does not work

function g()
       tmp2=[1,2]
       @SVector [1 for i in 1:length(tmp2)]
end
g()

ERROR: LoadError: UndefVarError: tmp2 not defined
Stacktrace:
 [1] top-level scope
   @ none:1
 [2] eval(m::Module, e::Any)
   @ Core ./boot.jl:368
 [3] var"@SVector"(__source__::LineNumberNode, __module__::Module, ex::Any)
   @ StaticArrays ~/.julia/packages/StaticArrays/0bweZ/src/SVector.jl:60
in expression starting at REPL[2]:3

The problem is the following line in the macros will not access non global scope variables.

rng = Core.eval(__module__, ex.args[2].args[2])
@martinholters
Copy link
Collaborator

The argument to @SVector is evaluated at macro expansion time. And that is necessary to have the size available at compilation time. So I don't think there is much to be done here, except better documentation maybe. E.g. try

tmp2=[1,2]
function g()
    @SVector [1 for i in 1:length(tmp2)]
end
tmp2="Hello world!"
g()

@dpinol
Copy link
Author

dpinol commented Mar 21, 2022

hi, thanks. Yes, since it's a limitation of the macro, a better documentation will be appreciated by users.

@mateuszbaran mateuszbaran linked a pull request Mar 23, 2022 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants