@@ -187,7 +187,8 @@ promote_result(::Type{T},::Type{S},::Type{Bottom},::Type{Bottom}) where {T,S} =
187
187
"""
188
188
promote(xs...)
189
189
190
- Convert all arguments to their common promotion type (if any), and return them all (as a tuple).
190
+ Convert all arguments to a common type, and return them all (as a tuple).
191
+ If no arguments can be converted, an error is raised.
191
192
192
193
# Examples
193
194
```jldoctest
@@ -197,21 +198,19 @@ julia> promote(Int8(1), Float16(4.5), Float32(4.1))
197
198
"""
198
199
function promote end
199
200
200
- promote () = ()
201
- promote (x) = (x,)
202
- function promote (x:: T , y:: S ) where {T,S}
201
+ function _promote (x:: T , y:: S ) where {T,S}
203
202
@_inline_meta
204
203
(convert (promote_type (T,S),x), convert (promote_type (T,S),y))
205
204
end
206
205
promote_typeof (x) = typeof (x)
207
206
promote_typeof (x, xs... ) = (@_inline_meta ; promote_type (typeof (x), promote_typeof (xs... )))
208
- function promote (x, y, z)
207
+ function _promote (x, y, z)
209
208
@_inline_meta
210
209
(convert (promote_typeof (x,y,z), x),
211
210
convert (promote_typeof (x,y,z), y),
212
211
convert (promote_typeof (x,y,z), z))
213
212
end
214
- function promote (x, y, zs... )
213
+ function _promote (x, y, zs... )
215
214
@_inline_meta
216
215
(convert (promote_typeof (x,y,zs... ), x),
217
216
convert (promote_typeof (x,y,zs... ), y),
@@ -240,39 +239,38 @@ promote_to_supertype(::Type{T}, ::Type{S}, ::Type{S}) where {T<:Number,S<:Number
240
239
promote_to_supertype (:: Type{T} , :: Type{S} , :: Type ) where {T<: Number ,S<: Number } =
241
240
error (" no promotion exists for " , T, " and " , S)
242
241
243
- # promotion with a check for circularity. Can be used to catch what
244
- # would otherwise become StackOverflowErrors.
245
- function promote_noncircular (x, y)
242
+ promote () = ()
243
+ promote (x) = (x,)
244
+
245
+ function promote (x, y)
246
246
@_inline_meta
247
- px, py = promote (x, y)
248
- not_all_sametype ((x,px ), (y ,py))
247
+ px, py = _promote (x, y)
248
+ not_sametype ((x,y ), (px ,py))
249
249
px, py
250
250
end
251
- function promote_noncircular (x, y, z)
251
+ function promote (x, y, z)
252
252
@_inline_meta
253
- px, py, pz = promote (x, y, z)
254
- not_all_sametype ((x,px), (y,py ), (z ,pz))
253
+ px, py, pz = _promote (x, y, z)
254
+ not_sametype ((x,y,z ), (px,py ,pz))
255
255
px, py, pz
256
256
end
257
- function promote_noncircular (x, y, z, a... )
258
- p = promote (x, y, z, a... )
259
- not_all_sametype ( map (identity, ( x, y, z, a... ), p) )
257
+ function promote (x, y, z, a... )
258
+ p = _promote (x, y, z, a... )
259
+ not_sametype (( x, y, z, a... ), p)
260
260
p
261
261
end
262
- not_all_sametype (x, y) = nothing
263
- not_all_sametype (x, y, z) = nothing
264
- not_all_sametype (x:: Tuple{S,S} , y:: Tuple{T,T} ) where {S,T} = sametype_error (x[1 ], y[1 ])
265
- not_all_sametype (x:: Tuple{R,R} , y:: Tuple{S,S} , z:: Tuple{T,T} ) where {R,S,T} = sametype_error (x[1 ], y[1 ], z[1 ])
266
- function not_all_sametype (:: Tuple{R,R} , y:: Tuple{S,S} , z:: Tuple{T,T} , args... ) where {R,S,T}
267
- @_inline_meta
268
- not_all_sametype (y, z, args... )
269
- end
270
- not_all_sametype () = error (" promotion failed to change any input types" )
271
- function sametype_error (input... )
262
+
263
+ promote (x:: T , y:: T , zs:: T... ) where {T} = (x, y, zs... )
264
+
265
+ not_sametype (x:: T , y:: T ) where {T} = sametype_error (x)
266
+
267
+ not_sametype (x, y) = nothing
268
+
269
+ function sametype_error (input)
272
270
@_noinline_meta
273
- error (" circular method definition: promotion of types " ,
271
+ error (" promotion of types " ,
274
272
join (map (x-> string (typeof (x)), input), " , " , " and " ),
275
- " failed to change any input types " )
273
+ " failed to change any arguments " )
276
274
end
277
275
278
276
+ (x:: Number , y:: Number ) = + (promote (x,y)... )
@@ -389,3 +387,5 @@ minmax(x::Real) = (x, x)
389
387
max (x:: T , y:: T ) where {T<: Real } = select_value (y < x, x, y)
390
388
min (x:: T , y:: T ) where {T<: Real } = select_value (y < x, y, x)
391
389
minmax (x:: T , y:: T ) where {T<: Real } = y < x ? (y, x) : (x, y)
390
+
391
+ flipsign (x:: T , y:: T ) where {T<: Signed } = no_op_err (" flipsign" , T)
0 commit comments