Broadcast with container for multidimensional array

Hi,

I’ve got a container type for a multidimensional array, and I want to redirect the iterate function to the ‘inner’ array.
This works fine, except it does not preserve dimensions when combined with a broadcast call.
I’m probably missing something very basic here, but I can only seem to get it to broadcast linearly…

E.g.:

struct A
	arr::Array{Bool}
end

Base.iterate(a::A) = iterate(a.arr)
Base.iterate(a::A, state) = iterate(a.arr, state)
Base.length(a::A) = length(a.arr)

x = fill(false, 1,2,3)
# typeof(x) == Array{Bool,3}
println(.!x)
# Bool [1 1]
# Bool [1 1]
# Bool [1 1]
# -> makes sense

a = A(x)
# typeof(a) == A
println(.!a)
# Bool [1, 1, 1, 1, 1, 1]
# -> not what I'd expect

Thanks for your help :slight_smile:

Now that I think about it again, the problem seems to be more broadcast related…
My iterate function does what it is supposed to do: iterate over every element in my array.
So the real question is: How do i get the broadcast operation for my container type to work like the broadcast on the ‘inner’ type?

EDIT: edited the original question to match my new insights :smiley:

Essentially the problem is that you’ve not “taught” Julia what the shape of your custom struct is yet. The only thing that Julia knows is that it’s iterable with a one-dimensional length.

Depending upon what you’re doing, you may want to define its size or even subtype <: AbstractMatrix{Bool}. Many many more details in the interfaces chapter: Interfaces · The Julia Language

1 Like

Thank you, that was the page I needed, and the function I was missing was Base.IteratorSize :slight_smile: