# How To Properly Use Unitful

**URL:** <https://discourse.julialang.org/t/how-to-properly-use-unitful/40295>\
**Category:** General Usage\
**Tags:** question, unitful\
**Created:** [May 27, 2020, 10:24pm UTC](https://discourse.julialang.org/t/how-to-properly-use-unitful/40295 "2020-05-27T22:24:24Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![emmt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmt/32/5192_2.png) [@emmt](https://discourse.julialang.org/u/emmt)\
**Post date:** [October 31, 2023, 2:58pm UTC](https://discourse.julialang.org/t/how-to-properly-use-unitful/40295/14 "2023-10-31T14:58:36Z")

</div>

I ran into the same issue. Below I describe how I inverstigated the problem and fixed the type definition (spoiler: solution is at the end).

```julia
julia> using Unitful

julia> a = typeof(1.0u"m")
Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}

```

OK so let us define type `b` (a supposed alias for type `a`) as (note that you need the `Unitful.` prefix unless you import symbols `m` and `𝐋`, a bold L, from `Unitful`):

```julia
julia> b = Quantity{Float64, Unitful.𝐋, Unitful.FreeUnits{(Unitful.m,), Unitful.𝐋, nothing}}
Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}

```

`a` and `b` are printed exactly the same however they are different as shown by:

```julia
julia> a == b
false

```

Exactly what was observed by @acdupont at the start of this discussion.

Since `a` and `b` are parametric types, let us find out which of their parameters is/are different by introspection:

```julia
julia> a.parameters .== b.parameters
3-element BitVector:
 1
 1
 0

```

So the culprit is the 3rd one, a `Unitful.FreeUnits` structure, we use the same trick as above to compare its parameters:

```julia
julia> a.parameters[3].parameters .== b.parameters[3].parameters
3-element BitVector:
 0
 1
 1

```

So, the 1st parameter is now the one which is different. However:

```julia
julia> a.parameters[3].parameters[1]
(m,)

julia> b.parameters[3].parameters[1]
(m,)

```

which (wrongly) seems to indicate that both are 1-tuple with single element `m`. Let us check the type of the element:

```julia
julia> typeof(a.parameters[3].parameters[1][1])
Unitful.Unit{:Meter, 𝐋}

julia> typeof(b.parameters[3].parameters[1][1])
Unitful.FreeUnits{(m,), 𝐋, nothing}

```

Hooray, we finally found a difference!

Unrolling all this, the correct definition for `b` is:

```julia
julia> b = Quantity{Float64, Unitful.𝐋, Unitful.FreeUnits{(Unitful.Unit{:Meter,Unitful.𝐋}(0,1//1),), Unitful.𝐋, nothing}}
Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}

```

Let us check this:

```julia
julia> a == b
true

julia> a === b
true

```

To give a practical example that can be used to define precisely the type of a field in a structure (the original need), we can define the following type alias:

```julia
const Meters{T<:Real} = Quantity{T, Unitful.𝐋, Unitful.FreeUnits{(Unitful.Unit{:Meter,Unitful.𝐋}(0,1//1),), Unitful.𝐋, nothing}}

```

and check that:

```julia
julia> typeof(1.0u"m") === Meters{Float64}
true

julia> typeof(1u"m") === Meters{Int}
true

julia> convert(Meters{Float32}, 2.3u"mm")
0.0023f0 m

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-properly-use-unitful/40295)._
