196196
197197Base. similar (A:: QuantityArray ) = QuantityArray (similar (ustrip (A)), dimension (A), quantity_type (A))
198198Base. similar (A:: QuantityArray , :: Type{S} ) where {S} = QuantityArray (similar (ustrip (A), S), dimension (A), quantity_type (A))
199+ for (type, _, _) in ABSTRACT_QUANTITY_TYPES
200+ @eval Base. similar (A:: QuantityArray , :: Type{S} ) where {S<: $type } = QuantityArray (similar (ustrip (A), value_type (S)), dimension (A), S)
201+ end
199202
200203# Unfortunately this mess of `similar` is required to avoid ambiguous methods.
201204# c.f. base/abstractarray.jl
211214
212215# `_similar_for` in Base does not account for changed dimensions, so
213216# we need to overload it for QuantityArray.
214- Base. _similar_for (c:: QuantityArray , :: Type{T} , itr, :: Base.HasShape , axs) where {T} =
217+ Base. _similar_for (c:: QuantityArray , :: Type{T} , itr, :: Base.HasShape , axs) where {T<: UnionAbstractQuantity } =
215218 QuantityArray (similar (ustrip (c), value_type (T), axs), dimension (materialize_first (itr)):: dim_type (T), T)
219+ Base. _similar_for (c:: QuantityArray , :: Type{T} , itr, :: Base.HasShape , axs) where {T} =
220+ similar (ustrip (c), T, axs)
216221
217222# These methods are not yet implemented, but the default implementation is dangerous,
218223# as it may cause a stack overflow, so we raise a more helpful error instead.
@@ -223,8 +228,10 @@ Base._similar_for(::QuantityArray, ::Type{T}, _, ::Base.HasLength, ::Integer) wh
223228
224229# In earlier Julia, `Base._similar_for` has different signatures.
225230@static if hasmethod (Base. _similar_for, Tuple{Array,Type,Any,Base. HasShape})
226- @eval Base. _similar_for (c:: QuantityArray , :: Type{T} , itr, :: Base.HasShape ) where {T} =
231+ @eval Base. _similar_for (c:: QuantityArray , :: Type{T} , itr, :: Base.HasShape ) where {T<: UnionAbstractQuantity } =
227232 QuantityArray (similar (ustrip (c), value_type (T), axes (itr)), dimension (materialize_first (itr)):: dim_type (T), T)
233+ @eval Base. _similar_for (c:: QuantityArray , :: Type{T} , itr, :: Base.HasShape ) where {T} =
234+ similar (ustrip (c), T, axes (itr))
228235end
229236@static if hasmethod (Base. _similar_for, Tuple{Array,Type,Any,Base. HasLength})
230237 @eval Base. _similar_for (:: QuantityArray , :: Type{T} , _, :: Base.HasLength ) where {T} =
0 commit comments