Loop Fusion usage between Arrays of custom structs

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.

This is how I would write your code:

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
2 Likes