196
196
197
197
Base. similar (A:: QuantityArray ) = QuantityArray (similar (ustrip (A)), dimension (A), quantity_type (A))
198
198
Base. 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
199
202
200
203
# Unfortunately this mess of `similar` is required to avoid ambiguous methods.
201
204
# c.f. base/abstractarray.jl
211
214
212
215
# `_similar_for` in Base does not account for changed dimensions, so
213
216
# 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 } =
215
218
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)
216
221
217
222
# These methods are not yet implemented, but the default implementation is dangerous,
218
223
# 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
223
228
224
229
# In earlier Julia, `Base._similar_for` has different signatures.
225
230
@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 } =
227
232
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))
228
235
end
229
236
@static if hasmethod (Base. _similar_for, Tuple{Array,Type,Any,Base. HasLength})
230
237
@eval Base. _similar_for (:: QuantityArray , :: Type{T} , _, :: Base.HasLength ) where {T} =
0 commit comments