From 7a7793a849f622ac89e98a002fd7043dd38091d3 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Thu, 9 Jan 2025 08:06:15 +0100 Subject: [PATCH] use dispatch instead of explicit subtyping checks --- base/broadcast.jl | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/base/broadcast.jl b/base/broadcast.jl index 9e99c8b8376f0..d1e3a386b6620 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -281,20 +281,14 @@ end Base.IteratorSize(::Type{T}) where {T<:Broadcasted} = Base.HasShape{ndims(T)}() Base.ndims(BC::Type{<:Broadcasted{<:Any,Nothing}}) = _maxndims_broadcasted(BC) -function Base.ndims(BC::Type{<:Broadcasted{<:AbstractArrayStyle{N},Nothing}}) where {N} - # `N` is the `AbstractArrayStyle` type parameter, so `N isa Union{Type,Int}`. - if (N isa Type) && (Any <: N) - # for `N isa Type`, `Any` is the only allowed value, indicating a wildcard - _maxndims_broadcasted(BC) - else - # for `N isa Int`, only nonnegative values are allowed, indicating the number of dimensions - let n = N::Int - if n < 0 - throw(ArgumentError("dimension count must not be negative")) - end - n - end +# the `AbstractArrayStyle` type parameter is required to be either equal to `Any` or be an `Int` value +Base.ndims(BC::Type{<:Broadcasted{<:AbstractArrayStyle{Any},Nothing}}) = _maxndims_broadcasted(BC) +function Base.ndims(::Type{<:Broadcasted{<:AbstractArrayStyle{N},Nothing}}) where {N} + n = N::Int + if n < 0 + throw(ArgumentError("dimension count must not be negative")) end + n end function _maxndims_broadcasted(BC::Type{<:Broadcasted})