Broadcasting structs as scalars

The following code used to work in Julia v0.6.4, but it is not working in Julia v1.0.

struct Q
       x::Int
end
f(q::Q, x::Int) = q.x + x
f.(Q(2), [1,2,3])
# ERROR: MethodError: no method matching length(::Q)

What should I do to broadcast on Q as a scalar?

Base.Broadcast.broadcastable(q::Q) = Ref(q) 
5 Likes

What’s the reasoning for not having something like this as the default for new types?

Also, wrapping as a Ref does not incur any performance costs?

2 Likes

Also:

Although it wouldn’t have helped in this special case, the advice is generally sound if you’re porting old code (older than 0.7) to 0.7/1.0.

1 Like

It also does not affect performance, Deprecate defaulting to scalar in broadcast (take 2) by mbauman · Pull Request #26435 · JuliaLang/julia · GitHub.