# \`\`\`@.\`\`\` throws unexpected error

**URL:** <https://discourse.julialang.org/t/throws-unexpected-error/39772>\
**Category:** General Usage\
**Tags:** broadcasting, convert\
**Created:** [May 19, 2020, 7:02pm UTC](https://discourse.julialang.org/t/throws-unexpected-error/39772 "2020-05-19T19:02:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henri\_Laurie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henri_laurie/32/5079_2.png) [@Henri\_Laurie](https://discourse.julialang.org/u/Henri_Laurie)\
**Post date:** [May 19, 2020, 7:02pm UTC](https://discourse.julialang.org/t/throws-unexpected-error/39772/1 "2020-05-19T19:02:15Z")

</div>

I get a somewhat odd error with @. MWE:

```julia
julia> y = Array{Float64}(undef, 2, 2); @. y =rand(2,2) + ones(2,2)
ERROR: MethodError: Cannot `convert` an object of type Array{Float64,2} to an object of type Float64

```

Where is the attempted conversion? Moving `@.` to after the assignment operator works fine, as does pre-allocating the two matrices.

---

<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:** [May 19, 2020, 7:32pm UTC](https://discourse.julialang.org/t/throws-unexpected-error/39772/2 "2020-05-19T19:32:40Z")

</div>

`@.` affects _every_ function call, which includes `ones` and `rand` in your example. The code is equivalent to:

```julia
y .= rand.(2, 2) .+ ones.(2, 2)

```

which roughly means: “for each index in y, do `y[I] = rand(2,2) + ones(2, 2)`” , which isn’t what you want.

In this case, you may want to just add the `.` to the functions you actually want to broadcast, as in:

```julia
y .= rand(2, 2) .+ ones(2, 2)

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [May 19, 2020, 9:43pm UTC](https://discourse.julialang.org/t/throws-unexpected-error/39772/3 "2020-05-19T21:43:31Z")

</div>

Incidentally, when trying to debug code with macros, `@macroexpand` (or `@macroexpand1`) is handy.  
It probably would have allowed you to find the problem in this case:

```julia
julia> @macroexpand @. y = rand(2,2) + ones(2,2)
:(y .= (+).(rand.(2, 2), ones.(2, 2)))

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 19, 2020, 9:55pm UTC](https://discourse.julialang.org/t/throws-unexpected-error/39772/4 "2020-05-19T21:55:58Z")

</div>

You can also suppress broadcasting with `$`, as in

```julia
@. y = $rand(2,2) + $ones(2,2)

```

Note that you don’t need to construct an array of `ones` here. You can just use `.+ 1` as in

```julia
@. y = $rand(2,2) + 1

```

In fact, you don’t need to construct the `rand(2,2)` array either. Just do:

```julia
 @. y = rand() + 1

```

and the `rand.()` call will call `rand()` separately for each element of `y`.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 19, 2020, 9:57pm UTC](https://discourse.julialang.org/t/throws-unexpected-error/39772/5 "2020-05-19T21:57:18Z")

</div>

Won’t the last one be slower than the previous? As I understood it, we have optimizations for generating multiple random numbers at once.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 19, 2020, 9:58pm UTC](https://discourse.julialang.org/t/throws-unexpected-error/39772/6 "2020-05-19T21:58:31Z")

</div>

> [@Oscar\_Smith](#):
>
> Won’t the last one be slower than the previous?

Allocating a 2x2 temporary array just to discard it is also slow; it’s a tradeoff. In general, I would err on the side of fewer temporary arrays and more readable code.
