Unitful.jl icon indicating copy to clipboard operation
Unitful.jl copied to clipboard

`unit`/`dimension` of quantity types without well-defined unit or dimension

Open sostock opened this issue 3 years ago • 1 comments

The unit and dimension functions return NoUnits/NoDims for quantity types without well-defined units/dimensions:

julia> unit(Quantity{Int}) # return `NoUnits`, which is not printed


julia> dimension(Quantity{Int})
NoDims

I consider this a bug, since NoUnits/NoDims represent an actual unit and an actual dimension. Using the same value NoDims for dimensionless objects (like 5u"m/mm", which has a well-defined dimension) and for objects without well-defined dimension (like Quantity{Int}) seems wrong to me.

Instead of a bug, this could also be seen as a pun on the NoUnits/NoDims values: They do not only represent dimensionless objects, but also objects without well-defined dimension. Still, using the same values for both of these concepts seems wrong to me. And I’m not convinced the pun is intentional. After all, calling unit/dimension on other values without well-defined dimensions does error:

julia> unit("foo")
ERROR: MethodError: no method matching unit(::String)
[…]

julia> dimension("bar")
ERROR: MethodError: no method matching dimension(::String)
[…]

Also, unit(Quantity{Int}) is hitting this method, which makes it seem more like an oversight than an intentional pun: https://github.com/PainterQubits/Unitful.jl/blob/f9992c08c5917f0cb6402bce26cd0a71a61ceb97/src/utils.jl#L133

sostock avatar Feb 26 '22 07:02 sostock

An even more confusing example (this one could return 𝐋 or error, but it definitely shouldn’t return NoDims):

julia> dimension(Unitful.Length)
NoDims

sostock avatar Apr 22 '24 09:04 sostock