Error when assigning a struct, via broadcast, to an array of structs

I’m assigning a value, a single value, to an array of structs using an array to do the indexing. Let me jump straight to my MWE and the punch line

[1] x[ii] .= a

gives an error message, but

[2] x[ii] .= [a]

does not. And it makes sense that it works when you look at the error message, as ‘[a]’ has a length and ‘a’ does not. What I can’t figure out, of course, is why the error message shows up in the first place. The same example that uses an array of Int, Float64, etc… instead of a struct doesn’t show the same behavior, i.e. I can use form [1] and not get an error.

struct Foo

x = Array{Foo,3}(undef,5,4,3)
y = randn(5,4,3)

for i=1:size(x,1)
    for j=1:size(x,2)
        for k=1:size(x,3)
            x[i,j,k] = Foo(y[i,j,k])

ii = y .< 0.0

a = Foo(0.0)

x[ii] .= a

ERROR: LoadError: MethodError: no method matching length(::Foo)
Closest candidates are:

You need

Base.Broadcast.broadcastable(q::Foo) = Ref(q)

Cf Broadcasting structs as scalars



that’s an interesting thread too.