I wonder what is the correct way to create objects that are iterable (or broadcastable?). The code below works as expected.
struct A
x::Float64
y::Float64
end
function foo(n,m,s::A)
u = n^2 + s.x^2
v = m + s.y
return u,v
end
a = A(1,2)
u,v = foo(4,5,a)
However now I would like to be able to pass vectors to foo so it can also return vectors eg:
x = collect(1:10)
y = collect(2:11)
u,v = foo.(x,y,a)
Note here that ‘a’ is still a single object of type ‘A’. I have tried to extend length and iterate among others but to no avail. Thanks!
jling
2
u,v = foo.(x,y,Ref(a))
You’re asking how to NOT “expand” your struct during broad cast. You’re not iterating over your object
1 Like
jling
4
maybe you want to revise your title a bit, but I’m not sure what’s the most likely titled to appear in google when someone searches this
1 Like
To make this work without the Ref
(i.e., to always treat objects of type A
as scalars when broadcasting), you can define
Base.Broadcast.broadcastable(a::A) = Ref(a)
6 Likes