# Unitful.jl: typeof() vs manually given types

**URL:** <https://discourse.julialang.org/t/unitful-jl-typeof-vs-manually-given-types/92957>\
**Category:** General Usage\
**Tags:** unitful\
**Created:** [January 14, 2023, 11:06am UTC](https://discourse.julialang.org/t/unitful-jl-typeof-vs-manually-given-types/92957 "2023-01-14T11:06:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DOT](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DOT](https://discourse.julialang.org/u/DOT)\
**Post date:** [January 14, 2023, 11:06am UTC](https://discourse.julialang.org/t/unitful-jl-typeof-vs-manually-given-types/92957/1 "2023-01-14T11:06:44Z")

</div>

Taking `typeof()` of some quantity, vs copy+pasting the output as a type definition – these two don’t seem to be equivalent:

```julia
> typeof(1.0s)
Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}

> 1.0s isa typeof(1.0s)
true

> 1.0s isa Quantity{Float64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}
false

```

What’s my mistake here?

Loads of people must have asked this, but I couldn’t find the answer… 😥

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 14, 2023, 12:26pm UTC](https://discourse.julialang.org/t/unitful-jl-typeof-vs-manually-given-types/92957/2 "2023-01-14T12:26:16Z")

</div>

```julia
julia> typeof(1.0s)==
  Quantity{
    Float64, 
    Unitful.𝐓, 
    Unitful.FreeUnits{
      (Unitful.Unit{:Second, Unitful.𝐓}(0,1),), 
      Unitful.𝐓, 
      nothing
    }
  }
true

```

The problem seems to be a mis-match between the way `1.0s`’s type is displayed and the type obtained by typing it as a literal. Specifically, `s` is `Unitful.Unit{:Second, Unitful.𝐓}(0,1)` and not a another `FreeUnits` type (note that `Unitful.s` is a `Unitful.FreeUnits{(s,), 𝐓, nothing}` type and thus just typing `s` or `Unitful.s` will not get the parameter of `typeof(1.0s)` right)

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 14, 2023, 12:26pm UTC](https://discourse.julialang.org/t/unitful-jl-typeof-vs-manually-given-types/92957/3 "2023-01-14T12:26:20Z")

</div>

```julia
julia> 1.0s isa Quantity{Float64, 𝐓 , Unitful.FreeUnits{(Unitful.Unit{:Second, 𝐓}(0, 1//1),), 𝐓 , nothing}}
true

```

The culprit here is are overloads of `show` in Unitful

```julia
function show(io::IO, x::Unit{N,D}) where {N,D}
    show(io, FreeUnits{(x,), D, nothing}())
end

```

in conjunction with

```julia
function show(io::IO, x::Unitlike)
    showoperators = get(io, :showoperators, false)
    first = ""
    sep = showoperators ? "*" : " "
    foreach(sortexp(typeof(x).parameters[1])) do y
        print(io,first)
        showrep(io,y)
        first = sep
    end
    nothing
end

```

which will make `Unitful.Unit{:Second, 𝐓}(0, 1//1)` print as `s`.

Question is, why are you comparing types this way? I’d imagine it’d be more robust to check `unit` and `dimension`. But then again, isn’t a major point of working with units needing _not_ to care about units?

---

<div class="post-metadata">

**Author:** ![DOT](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DOT](https://discourse.julialang.org/u/DOT)\
**Post date:** [January 14, 2023, 4:22pm UTC](https://discourse.julialang.org/t/unitful-jl-typeof-vs-manually-given-types/92957/4 "2023-01-14T16:22:10Z")

</div>

@Dan , @skleinbo: Thanks! 👍

---

<div class="post-metadata">

**Author:** ![cmichelenstrofer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmichelenstrofer/32/44392_2.png) [@cmichelenstrofer](https://discourse.julialang.org/u/cmichelenstrofer)\
**Post date:** [January 14, 2023, 8:38pm UTC](https://discourse.julialang.org/t/unitful-jl-typeof-vs-manually-given-types/92957/5 "2023-01-14T20:38:22Z")

</div>

I’ve found Julia types can get very long, especially when using _Unitful.jl_. There seems to be two things competing: having the information from `show` be accurate (you can copy-paste it and get the same quantity) and having it show something readable and informative. I don’t know what the best solution is, but I’ve gotten some truly nightmarish types when combining _AxisArrays_ and _Unitful_.
