Struct and vectorised function

struct X;  x end 
struct Y;  y end

f(::X,::Y) = 1

f.(  [X(1),X(1)], Y(1) )

This gives an error no method matching length(::Y)

Is there a way to tell it that the “.” vectorisation of f() is over the vector of X, not over Y ?

You can package Y(1) into a one element container. For Ref or tuple are common choices:

f.(  [X(1),X(1)], Ref(Y(1)) )
f.(  [X(1),X(1)], (Y(1),) )
2 Likes

For your own structs, you can also define Base.Broadcast.broadcastable to tell Julia to treat it as a scalar for broadcasting:

julia> Base.Broadcast.broadcastable(y::Y) = Ref(y)

julia> f(::X,::Y) = 1
f (generic function with 1 method)

julia> f.(  [X(1),X(1)], Y(1) )
2-element Vector{Int64}:
 1
 1
7 Likes