PainterQubits / Unitful.jl

Physical quantities with arbitrary units
Other
603 stars 110 forks source link

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

Open sostock opened 2 years ago

sostock commented 2 years ago

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 commented 4 months ago

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

julia> dimension(Unitful.Length)
NoDims