@@ -264,6 +264,17 @@ Base.LinearIndices(bc::Broadcasted{<:Any,<:Tuple{Any}}) = LinearIndices(axes(bc)
264264
265265Base. ndims (bc:: Broadcasted ) = ndims (typeof (bc))
266266Base. ndims (:: Type{<:Broadcasted{<:Any,<:NTuple{N,Any}}} ) where {N} = N
267+ Base. ndims (BC:: Type{<:Broadcasted{<:Any,Nothing}} ) = _maxndims (argtype (BC))
268+ function Base. ndims (BC:: Type{<:Broadcasted{<:AbstractArrayStyle{N},Nothing}} ) where {N}
269+ N isa Int ? N : _maxndims (argtype (BC))
270+ end
271+ _maxndims (:: Type{Tuple{}} ) = 0
272+ _maxndims (:: Type{Tuple{T}} ) where {T} = T <: Tuple ? 1 : Int (ndims (T)):: Int
273+ function _maxndims (Args:: Type{<:Tuple{T,Vararg}} ) where {T}
274+ m = T <: Tuple ? 1 : Int (ndims (T)):: Int
275+ n = _maxndims (Base. tuple_type_tail (Args))
276+ max (m, n)
277+ end
267278
268279Base. size (bc:: Broadcasted ) = map (length, axes (bc))
269280Base. length (bc:: Broadcasted ) = prod (size (bc))
@@ -280,20 +291,6 @@ Base.@propagate_inbounds function Base.iterate(bc::Broadcasted, s)
280291end
281292
282293Base. IteratorSize (:: Type{T} ) where {T<: Broadcasted } = Base. HasShape {ndims(T)} ()
283- Base. ndims (BC:: Type{<:Broadcasted{<:Any,Nothing}} ) = _maxndims_broadcasted (BC)
284- # the `AbstractArrayStyle` type parameter is required to be either equal to `Any` or be an `Int` value
285- Base. ndims (BC:: Type{<:Broadcasted{<:AbstractArrayStyle{Any},Nothing}} ) = _maxndims_broadcasted (BC)
286- Base. ndims (:: Type{<:Broadcasted{<:AbstractArrayStyle{N},Nothing}} ) where {N} = N:: Int
287-
288- function _maxndims_broadcasted (BC:: Type{<:Broadcasted} )
289- _maxndims (fieldtype (BC, :args ))
290- end
291- _maxndims (:: Type{T} ) where {T<: Tuple } = reduce (max, ntuple (n -> (F = fieldtype (T, n); F <: Tuple ? 1 : ndims (F)), Base. _counttuple (T)))
292- _maxndims (:: Type{<:Tuple{T}} ) where {T} = T <: Tuple ? 1 : ndims (T)
293- function _maxndims (:: Type{<:Tuple{T, S}} ) where {T, S}
294- return max (T <: Tuple ? 1 : ndims (T), S <: Tuple ? 1 : ndims (S))
295- end
296-
297294Base. IteratorEltype (:: Type{<:Broadcasted} ) = Base. EltypeUnknown ()
298295
299296# # Instantiation fills in the "missing" fields in Broadcasted.
0 commit comments