Unitful.jl
Unitful.jl copied to clipboard
typeof(Quantity) And The Result Are Not equal
I am experimenting with Unitful parametric types to force a particular unit in a struct or function. I am having trouble getting it to work, and in my experimenting, I discovered the following result:
julia> typeof(1.0u"cm^-3")
Quantity{Float64,𝐋^-3,Unitful.FreeUnits{(cm^-3,),𝐋^-3,nothing}}
julia> typeof(1.0u"cm^-3") <: typeof(1.0u"cm^-3")
true
julia> typeof(1.0u"cm^-3") <: Unitful.Quantity{Float64, Unitful.𝐋^-3,Unitful.FreeUnits{( Unitful.cm^-3,), Unitful.𝐋^-3,nothing}}
false
The result of the typeof(1.0u"cm^-3")
is not equal to typeof(1.0u"cm^-3")
, at least for the example above. In my struct, if I use typeof(1.0u"cm^-3")
as the type, I can use the constructor of the struct. But, when I use the result of typeof(1.0u"cm^-3")
, I get the following error:
ERROR: MethodError: no method matching Quantity{#s92495,𝐋^-3,cm^-3} where #s92495<:Real(::Quantity{Int64,𝐋^-3,Unitful.FreeUnits{(cm^-3,),𝐋^-3,nothing}})
There is a discourse discussion started on this: https://discourse.julialang.org/t/how-to-properly-use-unitful/40295
Just to add some information that has since been added to the Discourse thread, the difference is that although the printed representation is the same, the cm
in the third type parameter is not the same as Unitful.cm
:
julia> T1 = typeof(1.0u"cm^-3")
Quantity{Float64, 𝐋^-3, Unitful.FreeUnits{(cm^-3,), 𝐋^-3, nothing}}
julia> T2 = Unitful.Quantity{Float64, Unitful.𝐋^-3, Unitful.FreeUnits{(Unitful.cm^-3,), Unitful.𝐋^-3, nothing}}
Quantity{Float64, 𝐋^-3, Unitful.FreeUnits{(cm^-3,), 𝐋^-3, nothing}}
julia> typeof(T1.parameters[3].parameters[1][1])
Unitful.Unit{:Meter, 𝐋}
julia> typeof(T2.parameters[3].parameters[1][1])
Unitful.FreeUnits{(cm^-3,), 𝐋^-3, nothing}