JuliaPhysics / JuliaPhysics/Unitful.jl

typeof(Quantity) And The Result Are Not equal

Open
#339 1 comment 5 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
675
Forks
124
Avg merge
3h 38m
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the Julia REPL examples from the issue, including the subtype checks and struct-constructor error. Read the linked Discourse discussion for the intended behavior, then trace the Unitful parametric type handling; done means the reported type relationship and constructor use have a documented or verified resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.