I am trying to follow this blog which shows how to create a unit conversion code.
I have tried to define two temperate types Celsius
and Kelvin
and I define a promotion rule where Celsius
get promoted to Kelvin
. I also defined the conversion functions both way
abstract type Temperature end
struct Celsius <: Temperature
value::Float64
end
struct Kelvin <: Temperature
value::Float64
end
promote_rule(::Type{Kelvin}, ::Type{Celsius}) = Kelvin
convert(::Type{Kelvin}, t::Celsius) = Kelvin(t.value + 273.15)
convert(::Type{Celsius}, t::Kelvin) = Celsius(t.value - 273.15)
However, when I run
promote(Kelvin(1), Celsius(1))
I get the following error, what did I do wrong? Shouldn’t promote
promote the second argument which is of type Celsius
to Kelvin
?
ERROR: promotion of types Kelvin and Celsius failed to change any arguments
Stacktrace:
[1] error(::String, ::String, ::String) at .\error.jl:42
[2] sametype_error(::Tuple{Kelvin,Celsius}) at .\promotion.jl:308
[3] not_sametype(::Tuple{Kelvin,Celsius}, ::Tuple{Kelvin,Celsius}) at .\promotion.jl:302
[4] promote(::Kelvin, ::Celsius) at .\promotion.jl:285
[5] top-level scope at none:0