# StaticArray broadcast error

**URL:** <https://discourse.julialang.org/t/staticarray-broadcast-error/2134>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [February 16, 2017, 7:44am UTC](https://discourse.julialang.org/t/staticarray-broadcast-error/2134 "2017-02-16T07:44:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [February 16, 2017, 7:44am UTC](https://discourse.julialang.org/t/staticarray-broadcast-error/2134/1 "2017-02-16T07:44:28Z")

</div>

Hello,  
On julia 0.5.0, Win64, StaticArrays 0.2.1:  
I have 2 types defined as

```julia
using StaticArrays
immutable Node <: FieldVector{Float64}
    x::Float64
    y::Float64
    z::Float64
end
immutable Point3 <: FieldVector{Float64}
    x::Float64
    y::Float64
    z::Float64
end

```

and I’d like to do a vectorized math operation

```julia
p = Point3(1.0, 2.0, 3.0); n=Node(2.0, 4.0, 6.0)
d = n.-p
MethodError: no method matching similar_type(::Type{CST_Fields.Node}, ::Type{Float64})
Closest candidates are:
  similar_type{SA<:StaticArrays.StaticArray{T,N},N,T}(::Union{SA<:StaticArrays.StaticArray{T,N},Type{SA<:StaticArrays.StaticArray}}, ::Type{T}, !Matched::Tuple{Vararg{Int64,N}}) at c:\Users\phlavenk\Documents\programs\juliadata\v0.5\StaticArrays\src\abstractarray.jl:58
...
 in macro expansion at mapreduce.jl:24 [inlined]
 in map at mapreduce.jl:17 [inlined]
 in macro expansion at mapreduce.jl:246 [inlined]
 in broadcast at mapreduce.jl:243 [inlined]
 in ./(::CST_Fields.Node, ::CST_Fields.Point3) at arraymath.jl:13
...

```

Does anyone know, what to do for my types to behave well in the broadcast machinery?

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 16, 2017, 9:51am UTC](https://discourse.julialang.org/t/staticarray-broadcast-error/2134/2 "2017-02-16T09:51:29Z")

</div>

That’s not one I’ve seen before.

Just this week I’ve been working on cleaning up `similar_type` - it’s been completely overhauled. Could you please try to checkout the latest prerelease version by typing `Pkg.checkout("StaticArrays", "ajf/nopure")`? And let me know if the problem is fixed.

(You can revert to the standard version of StaticArrays with `Pkg.free("StaticArrays")`)

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 16, 2017, 9:56am UTC](https://discourse.julialang.org/t/staticarray-broadcast-error/2134/3 "2017-02-16T09:56:09Z")

</div>

PS - in general you can control the output type of operations via creating your own definitions for the `similar_type` function. There are brief instructions included with the new branch. Let me know if you need some more specifics.

---

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [February 16, 2017, 12:40pm UTC](https://discourse.julialang.org/t/staticarray-broadcast-error/2134/4 "2017-02-16T12:40:35Z")

</div>

OK, thank you for help. Hopefuly I understand.  
So in my case it is not clear what should be the result type of the math operation. Now, I can get the desired behavior by my own definition

```julia
similar_type{SA <: Union{Point3, Vec3, Node}, ElType<:AbstractFloat}(::Type{SA}, s::Type{ElType}) = SVector{Size(SA), ElType)

```

I think there should be a fallback function that errors with better error message telling what to do.  
Because there soon will be huge amount of people relying on this great package.  
Thank you.  
Petr

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 17, 2017, 10:21am UTC](https://discourse.julialang.org/t/staticarray-broadcast-error/2134/5 "2017-02-17T10:21:25Z")

</div>

Hopefully the improvements in the above-mentioned branch will be available via `Pkg.update()` sometime next week.
