JuliaPhysics / Measurements.jl

Error propagation calculator and library for physical measurements. It supports real and complex numbers with uncertainty, arbitrary precision calculations, operations with arrays, and numerical integration.

Home Page:https://juliaphysics.github.io/Measurements.jl/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Allow for More Flexible Unitful.jl Constructions

bhgomes opened this issue · comments

I think it would be great if it were possible to write:

using Measurements, Unitful, Unitful.DefaultSymbols
quantity = 1.3m ± 2.01μm

instead of

quantity = (1.3 ± 2.01e-6)m

The Julia REPL says that it would require a measurement(::Quantity, ::Quantity) function.

You can achieve what you'd like with the following definitions:

using Measurements, Unitful

function Measurements.measurement(a::T, b::T) where {T<:Quantity}
    u = unit(a)
    return measurement(ustrip(u, a), ustrip(u, b)) * u
end

Measurements.measurement(a::Quantity{T1,D,U1}, b::Quantity{T2,D,U2}) where {T1,T2,D,U1,U2} =
    measurement(promote(a, b)...)

E.g.

julia> quantity = 1.3km ± 2.01μm
1300.0 ± 2.01e-6 m

However I'm not going to add this feature because I don't wont to depend on Unitful.jl

I don't see why this couldn't be added as an optional function if Unitful.jl is already installed in the user's environment. This wouldn't add to your dependencies and it would be obviously helpful for anyone using both packages.

Yesterday I started playing to see if this feasible without using Unitful.jl: https://github.com/JuliaPhysics/Measurements.jl/tree/unitful. However I won't have much time to continue this experiment before next week

I don't see why this couldn't be added as an optional function if Unitful.jl is already installed in the user's environment

This isn't really straightforward without using some magic. One possible magic is using the Requires.jl which we happen to already load for something else. I'm going to open PR #36 to fix this issue.