Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeof(Quantity) And The Result Are Not equal #339

Open
acdupont opened this issue May 28, 2020 · 1 comment
Open

typeof(Quantity) And The Result Are Not equal #339

acdupont opened this issue May 28, 2020 · 1 comment

Comments

@acdupont
Copy link

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

@Socob
Copy link
Contributor

Socob commented Feb 4, 2024

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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants