promote
promote(xs...)
Convert all arguments to their common promotion type (if any), and return them all (as a tuple).
Examples
In the Julia programming language, the function promote(xs...)
is used to convert all arguments to their common promotion type (if any) and return them all as a tuple.
julia> promote(1.5, 2)
(1.5, 2.0)
In this example, both 1.5
and 2
are promoted to their common type, which is Float64
. The function returns a tuple (1.5, 2.0)
.
julia> promote(3, 4, 5)
(3, 4, 5)
In this case, all arguments are already of the same type (Int64
), so no promotion is needed. The function returns a tuple (3, 4, 5)
.
julia> promote(1, 2.5, 3//2)
(1.0, 2.5, 1.5)
Here, the arguments have different types (Int64
, Float64
, Rational{Int64}
), so they are promoted to their common type, which is Float64
. The function returns a tuple (1.0, 2.5, 1.5)
.
Common mistake example:
julia> promote(1, "two")
ERROR: MethodError: no promotion exists between Int64 and String
In this example, the arguments 1
and "two"
have incompatible types (Int64
and String
). The promote
function requires a common promotion type, and if it doesn't exist, it throws a MethodError
. Ensure that the arguments can be promoted to a common type before using the promote
function.
See Also
base, big, bytes2hex, cconvert, complex, convert, dec, hex, hex2bytes, hex2num, oct, oftype, parse, promote, signed, unsafe_convert, unsigned, widen,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.