UndefVarError with @. macro. Needs to pre-declare the assigned variable?



I’m fairly new to Julia (my main language is Python). I’m struggling with a problem that is probably pretty basic with regard to macro usage. I’m on Julia 0.6.2.

In particular for the @. macro, if I run:

@. x = (abs([1 2 3]) <= 1)

I get a ERROR: UndefVarError: x not defined. If x is defined before, this runs fine. Is it a well-know fact that the assigned variable needs to be predeclared for using with @.?

I’ve found seamingly related questions on StackOverflow (Broken macro functionality in Julia v0.6 and Julia macro expansion inside function) and a PR 15850 about a change of “macro hygiene” in 0.6 that generated similar UndefVarError errors in various modules. But in the end, I cannot relate these to my problem.

Thanks for your kind help.

julia> x = @. (abs([1 2 3]) <= 1)
1×3 BitArray{2}:
 true  false  false

x .= is inplace assignment.

@. x = (abs([1 2 3]) <= 1)

expands to the equivalent of (roughly)

let z = [1, 2, 3]
    for i in 1:3 
        x[i] = abs(z[i] <= 1)

so x needs to exist.


Thanks a lot!

I had missed the fact that = was also “dotted” by the @. macro, and I didn’t have in mind that .= is inplace assignement.