# How to provide two versions of some quantities to the user. Can they unload a module?

**URL:** https://discourse.julialang.org/t/how-to-provide-two-versions-of-some-quantities-to-the-user-can-they-unload-a-module/57325
**Category:** General Usage
**Tags:** unitful
**Created:** [March 16, 2021, 6:53pm UTC](https://discourse.julialang.org/t/how-to-provide-two-versions-of-some-quantities-to-the-user-can-they-unload-a-module/57325 "2021-03-16T18:53:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [March 16, 2021, 6:53pm UTC](https://discourse.julialang.org/t/how-to-provide-two-versions-of-some-quantities-to-the-user-can-they-unload-a-module/57325/1 "2021-03-16T18:53:03Z")

</div>

Hi! I wrote this on the Slack but I think here may be a good fit.

I have a package that contains some physical constants (for example g, the acceleration of gravity). I provide units with the constants through `Unitful.jl` . But I want the user to be able to use the constants with units or without units

One option is to leave the user the burden of importing the module and defining all the unitless constants by hand at the beginning. But because my user is me, I found this annoying. What I thought is to have two submodules on my package. One contains the values with units and one without units.

```julia
module MyPackage
    module WithUnits
         export constantsWithUnits
    end
    module NoUnits 
        export constantsWithNoUnits
    end
end

```

So that the user can do at the beginning:

```julia
using MyPackage
using MyPackage.WithUnits

```

or

```julia
using MyPackage
using MyPackage.NoUnits

```

and get the constants with units or without them at will.

This seems to work, but then how can I change my choice after I used one of them already? This would be useful for example to run the unit tests with both of the choices.

What other design ideas come to mind?

Thanks

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 16, 2021, 7:56pm UTC](https://discourse.julialang.org/t/how-to-provide-two-versions-of-some-quantities-to-the-user-can-they-unload-a-module/57325/2 "2021-03-16T19:56:08Z")

</div>

You could bind a non-constant name to the module, but probably that cannot be recommended if you need these constants for performance critical code:

```julia
julia> module A
         export c
         c = 1
       end
Main.A

julia> module B
         export c
         c = 2
       end
Main.B

julia> using .A

julia> mymod = A
Main.A

julia> mymod.c
1

julia> using .B

julia> mymod = B
Main.B

julia> mymod.c
2

```

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [March 16, 2021, 11:34pm UTC](https://discourse.julialang.org/t/how-to-provide-two-versions-of-some-quantities-to-the-user-can-they-unload-a-module/57325/3 "2021-03-16T23:34:23Z")

</div>

I think this may work, although I wanted to avoid having to qualify constants name with a module, e.g. instead of `g` having to write `constants.g` which makes it many times longer. I should rethink my design and keep an eye open on other packages to see how they do similar things.

---

<div class="post-metadata">

### Author: ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)
#### Post date: [March 18, 2021, 3:15pm UTC](https://discourse.julialang.org/t/how-to-provide-two-versions-of-some-quantities-to-the-user-can-they-unload-a-module/57325/4 "2021-03-18T15:15:37Z")

</div>

Hi,  
This is something for which I’m also looking for a solution.  
I was thinking more of another way, but maybe it’s not applicable to your case.

```julia
using Unitful
import PhysicalConstants.CODATA2018: c_0

```

Then define two methods that give back c\_0:

```julia
speed_light( :: T ) where {T <: Number} = ustrip(c_0)
speed_light( ::Quantity{T,D,U}) where {T,D,U} = Float64(c_0)

```

The first method gives just the value of c\_0, the second adds the units:

```julia
julia> speed_light(1.0)
2.99792458e8

julia> speed_light(1u"m")
2.99792458e8 m s^-1

```

Now you could use this in your code to compute the frequency

```julia
julia>frequency(wavelength) = speed_light(wavelength)/wavelength
frequency (generic function with 1 method)

julia> frequency(200e-9)
1.49896229e15

frequency(200e-9u"m")
1.49896229e15 s^-1

```

No need for two modules.  
Would that work for you?
