# Loop Fusion usage between Arrays of custom structs

**URL:** <https://discourse.julialang.org/t/loop-fusion-usage-between-arrays-of-custom-structs/18969>\
**Category:** General Usage\
**Tags:** loops\
**Created:** [December 25, 2018, 3:09am UTC](https://discourse.julialang.org/t/loop-fusion-usage-between-arrays-of-custom-structs/18969 "2018-12-25T03:09:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![algebrato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algebrato/32/6778_2.png) [@algebrato](https://discourse.julialang.org/u/algebrato)\
**Post date:** [December 25, 2018, 3:09am UTC](https://discourse.julialang.org/t/loop-fusion-usage-between-arrays-of-custom-structs/18969/1 "2018-12-25T03:09:57Z")

</div>

Hello,  
Is possible to use the Loop Fusion between arrays of custom defined structures? For example:

```julia
struct point{T}
  x::T
  y::T
end

x = rand(100)
y = rand(100)

p = point{Array{Float64,1}}(x,y)

```

at this point I created a 100 elements array of points, and then i overload the + operator

```julia
Base.:+(a::Point{Float64}, b::Point{Float64}) = Point{Float64}(a.x+b.x , a.y+b.y)

```

then i defined another point and i tried to add it to every elements of array using .+ loop fusion operator

```julia
point_p = point{Float64}(1,1)
p_new .= p .+ point_p

```

and return the following error:

ERROR: LoadError: MethodError: no method matching length(::point{Array{Float64,1}})  
Closest candidates are:  
length(!Matched::Core.SimpleVector) at essentials.jl:582  
length(!Matched::Base.MethodList) at reflection.jl:732  
length(!Matched::Core.MethodTable) at reflection.jl:806

Is possible to overload the loop fusion . operator?

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [December 25, 2018, 4:04am UTC](https://discourse.julialang.org/t/loop-fusion-usage-between-arrays-of-custom-structs/18969/2 "2018-12-25T04:04:45Z")

</div>

First, what you create in `p` is a single `point` object, with 100 element `x` and `y` vectors. I don’t think this is what you want – you probably want a vector with 100 `Point` objects?

Second, unless I’m mistaken, to use the dot operators (broadcasting) with custom types, you need to wrap it in `Ref` in order to treat it as a scalar (or put it in a tuple or array). This was discussed at length in [this topic](https://discourse.julialang.org/t/recent-broadcast-changes-iterate-by-default-scalar-struct-and/11178).

This is how I would write your code:

```julia
struct Point{T}
  x::T
  y::T
end

points = [Point(rand(), rand()) for n = 1:5]

Base.:+(a::Point, b::Point) = Point(a.x+b.x , a.y+b.y)

point = Point(1, 1)

# these all give the same result, pick one:
points .+ Ref(point)
points .+ (point,)
points .+ [point] # note: extra allocation, slightly slower

```
