Skip to content

Commit

Permalink
use dispatch instead of explicit subtyping checks
Browse files Browse the repository at this point in the history
  • Loading branch information
nsajko committed Jan 9, 2025
1 parent 5f43d7d commit 7a7793a
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit 7a7793a

Please sign in to comment.