9797scalar_getindex (x) = x
9898scalar_getindex (x:: Ref ) = x[]
9999
100- @generated function _broadcast (f, :: Size{newsize} , s:: Tuple{Vararg{Size}} , a... ) where newsize
101- first_staticarray = a[findfirst (ai -> ai <: Union{StaticArray, Transpose{<:Any, <:StaticArray}, Adjoint{<:Any, <:StaticArray}, Diagonal{<:Any, <:StaticArray}} , a)]
100+ isstatic (:: StaticArray ) = true
101+ isstatic (:: Transpose{<:Any, <:StaticArray} ) = true
102+ isstatic (:: Adjoint{<:Any, <:StaticArray} ) = true
103+ isstatic (:: Diagonal{<:Any, <:StaticArray} ) = true
104+ isstatic (_) = false
102105
106+ @inline first_statictype (x, y... ) = isstatic (x) ? typeof (x) : first_statictype (y... )
107+ first_statictype () = error (" unresolved dest type" )
108+
109+ @inline function _broadcast (f, sz:: Size{newsize} , s:: Tuple{Vararg{Size}} , a... ) where newsize
110+ first_staticarray = first_statictype (a... )
103111 if prod (newsize) == 0
104112 # Use inference to get eltype in empty case (see also comments in _map)
105- eltys = [:(eltype (a[$ i])) for i ∈ 1 : length (a)]
106- return quote
107- @_inline_meta
108- T = Core. Compiler. return_type (f, Tuple{$ (eltys... )})
109- @inbounds return similar_type ($ first_staticarray, T, Size (newsize))()
110- end
113+ eltys = Tuple{map (eltype, a)... }
114+ T = Core. Compiler. return_type (f, eltys)
115+ @inbounds return similar_type (first_staticarray, T, Size (newsize))()
111116 end
117+ elements = __broadcast (f, sz, s, a... )
118+ @inbounds return similar_type (first_staticarray, eltype (elements), Size (newsize))(elements)
119+ end
112120
121+ @generated function __broadcast (f, :: Size{newsize} , s:: Tuple{Vararg{Size}} , a... ) where newsize
113122 sizes = [sz. parameters[1 ] for sz ∈ s. parameters]
114123 indices = CartesianIndices (newsize)
115124 exprs = similar (indices, Expr)
@@ -123,8 +132,7 @@ scalar_getindex(x::Ref) = x[]
123132
124133 return quote
125134 @_inline_meta
126- @inbounds elements = tuple ($ (exprs... ))
127- @inbounds return similar_type ($ first_staticarray, eltype (elements), Size (newsize))(elements)
135+ @inbounds return elements = tuple ($ (exprs... ))
128136 end
129137end
130138
0 commit comments