# @.  and UndefVarError

**URL:** <https://discourse.julialang.org/t/and-undefvarerror/19399>\
**Category:** New to Julia\
**Created:** [January 8, 2019, 9:59pm UTC](https://discourse.julialang.org/t/and-undefvarerror/19399 "2019-01-08T21:59:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hermann\_Eberl](https://avatars.discourse-cdn.com/v4/letter/h/fbc32d/32.png) [@Hermann\_Eberl](https://discourse.julialang.org/u/Hermann_Eberl)\
**Post date:** [January 8, 2019, 9:59pm UTC](https://discourse.julialang.org/t/and-undefvarerror/19399/1 "2019-01-08T21:59:41Z")

</div>

I have here a simple snippet of an example function that works just fine, i.e it returns what I expect it to return when i call it with a 2D argument x and a 1D argument p.

@. function dummy(x,p)  
3.0\*x[:,2] +p[1] +x[:,1]\*p[2]  
end

If instead i say (for example because i want to use the result of this calculation in the same function for further calculations)

@. function dummy(x,p)  
w= 3.0\*x[:,2] +p[1] +x[:,1]\*p[2]  
return w  
end

then I get an “UndefVarError: w not defined”. so far so good.

however, i have tried all sorts of ways that I can think of defining w (as a local array of appropriate shape, i.e. an array of the length of the first dimension of x) but the error message remains always the same. even if i try to define w as an obviously wrong type, the error message is “not defined” as opposed to some form of “nonsense defined”

how do i define a local w correctly in julia for use in a @. function?

thanks

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 8, 2019, 10:09pm UTC](https://discourse.julialang.org/t/and-undefvarerror/19399/2 "2019-01-08T22:09:38Z")

</div>

You don’t need to apply `@.` to the _entire_ function (and I wouldn’t recommend doing so, for exactly the reason you’ve run into here). The problem is that `@.` turns your `=` into `.=`, which in turn transforms `w = ...` into `w .= ...` which tries to broadcast the result into the (non-existent) array `w`.

Instead, just do:

```julia
function dummy(x, p)
  w = @.(3.0*x[:,2] +p[1] +x[:,1]*p[2])
  return w
end

```

Also, please [quote your code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) so that it will render correctly.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [January 8, 2019, 10:10pm UTC](https://discourse.julialang.org/t/and-undefvarerror/19399/3 "2019-01-08T22:10:45Z")

</div>

Not sure why, but the parser seems to be sensitive to a particular space here:

```julia
julia> x = randn(10,10); p = randn(10);
julia> @. 3.0*x[:,2] + p[1] + x[:,1] * p[2]
10-element Array{Float64,1}:
 -2.9666319136896138 
  2.7282891603028543 
 -4.853840108488322  
  0.6058019669739523 
 -1.2711187026514819 
  0.07949352735277701
  1.151771108029404  
  3.6311609306720025 
  1.156418356474127  
 -2.5863095768830022 

julia> @. 3.0*x[:,2] +p[1] + x[:,1] * p[2]
ERROR: LoadError: MethodError: no method matching @ __dot__ (::LineNumberNode, ::Module, ::Expr, ::Expr)
Closest candidates are:
  @ __dot__ (::LineNumberNode, ::Module, ::Any) at broadcast.jl:1145
in expression starting at REPL[24]:1

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [January 9, 2019, 4:09am UTC](https://discourse.julialang.org/t/and-undefvarerror/19399/4 "2019-01-09T04:09:38Z")

</div>

> [@jw3126](#):
>
> Not sure why, but the parser seems to be sensitive to a particular space here:

It’s because macro argument parsing is a space sensitive parsing context, and the parser uses the space to disambiguate unary vs binary `+`. Compare:

```julia
julia> :(@a y +x).args[3:end]
2-element Array{Any,1}:
 :y   
 :(+x)

julia> :(@a y + x).args[3:end]
1-element Array{Any,1}:
 :(y + x)

```

Some kind of space sensitive disambiguation is more obviously necessary when you consider unary `-`.
