# Julia's type system : what am I missing?

**URL:** https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618
**Category:** General Usage
**Tags:** question
**Created:** [July 10, 2025, 8:00pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618 "2025-07-10T20:00:41Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [July 10, 2025, 8:00pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/1 "2025-07-10T20:00:41Z")

</div>

Consider this MWE:

```julia
using Unitful

# 1. Get the type of a quantity constructed from the Unitful macro
t1 = typeof(1u"m")

# 2. Manually construct a type with the same structure
t2 = Unitful.Quantity{Int64, Unitful.𝐋, Unitful.FreeUnits{(Unitful.m,), Unitful.𝐋, nothing}}

# 3. Compare
println("t1 == t2: ", t1 == t2) # false – structurally different
println("t1 === t2: ", t1 === t2) # false – different type objects in memory

```

---

<div class="post-metadata">

### Author: ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)
#### Post date: [July 10, 2025, 8:11pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/2 "2025-07-10T20:11:11Z")

</div>

This has come up before, though I can’t find the other thread at the moment. I think the type isn’t being printed faithfully. You can do:

```julia
t3 = Quantity{Int64, dimension(u"m"), typeof(u"m")}

@show t1 == t3 # true
@show t1 === t3 # also true

```

---

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [July 10, 2025, 8:14pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/3 "2025-07-10T20:14:42Z")

</div>

Thanks for the response; this is a very subtle issue that took a long time to track down. Since dispatch depends on the faithful comparison of types, what is a good strategy to prevent this problem?

---

<div class="post-metadata">

### Author: ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)
#### Post date: [July 10, 2025, 8:18pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/4 "2025-07-10T20:18:01Z")

</div>

Here’s a related thread: [How To Properly Use Unitful - #12 by ajkeller34](https://discourse.julialang.org/t/how-to-properly-use-unitful/40295/12)

---

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [July 10, 2025, 8:21pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/5 "2025-07-10T20:21:27Z")

</div>

OMG 🙂

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [July 10, 2025, 8:29pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/6 "2025-07-10T20:29:48Z")

</div>

Wow, that’s very cursed. It’s the difference between:

```Julia-repl
julia> Unitful.FreeUnits{(Unitful.Unit{:Meter, Unitful.𝐋}(0, 1//1),), Unitful.𝐋, nothing}()
m

julia> Unitful.Unit{:Meter, Unitful.𝐋}(0, 1//1)
m

```

Both of those things print as `m`, but only one of them is the same as `Unitful.m`.

```Julia-repl
julia> typeof(Unitful.m)
Unitful.FreeUnits{(m,), 𝐋, nothing}

```

---

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [July 10, 2025, 8:29pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/7 "2025-07-10T20:29:56Z")

</div>

In the end I wanted to do something like this:

```julia
const DeviceUnits{T} = Union{
    Quantity{T, dimension(u"m"), typeof(u"m")},
    Quantity{T, dimension(u"cm"), typeof(u"cm")}} where T <: Number

```

and there was no easy way to construct the types. This works but the prior construction which used

```julia
Quantity{Int64, Unitful.𝐋, Unitful.FreeUnits{(Unitful.m,), Unitful.𝐋, nothing}}

```

did not.

I often find myself fighting the Julia type system.

---

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [July 10, 2025, 8:33pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/8 "2025-07-10T20:33:48Z")

</div>

I’m not adept enough to understand the mechanism that enables this.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [July 10, 2025, 8:43pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/9 "2025-07-10T20:43:34Z")

</div>

It’s not about Julia’s type system; it’s that Unitful is choosing to print two subtly different values as the same thing. And then it also defines a variable with the same name as what prints out.

It just becomes particularly confusing when you put these values into a type parameter. Here’s what’s effectively happening:

By default, Julia prints different values differently:

```julia-repl
julia> struct A end

julia> struct B end

julia> A()
A()

julia> B()
B()

```

But you can override that:

```julia-repl
julia> Base.show(io::IO, ::A) = print(io, "something")

julia> Base.show(io::IO, ::B) = print(io, "something")

julia> A()
something

julia> B()
something

```

And then it becomes even more confusing if that value ends up as a parameter of a type… or if you have another variable named `something`.

```julia-repl
julia> t1 = Val{A()}
Val{something}

julia> t2 = Val{B()}
Val{something}

julia> t1 == t2
false

```

---

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [July 10, 2025, 8:46pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/10 "2025-07-10T20:46:06Z")

</div>

aha, this makes dispatch with Unitful types particularly painful. I read in the docs that they recommend avoiding it. Thanks for the response!

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [July 10, 2025, 9:36pm UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/11 "2025-07-10T21:36:53Z")

</div>

I don’t know if this was missed in the discussion, but I frequently use dispatch on the supertypes like **Unitful.Length** based on the SI dimensions.

```julia
foo(x::Unitful.Length) = "Is a Unit of Length"
foo(x::Unitful.AbstractQuantity) = "Is NOT a Unit of Length"

julia> foo(1u"m")
"Is a Unit of Length"

julia> foo(1.0u"ft")
"Is a Unit of Length"

julia> foo(1u"kg")
"Is NOT a Unit of Length"

```

If not already defined, the `@derived_dimension` macro can be used for a different combination of SI dimensions.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [July 11, 2025, 3:41am UTC](https://discourse.julialang.org/t/julias-type-system-what-am-i-missing/130618/12 "2025-07-11T03:41:44Z")

</div>

Adding to @Daniel_Berge 's answer, you can easily dispatch on more specific aliases:

```julia
const Len{T} = Quantity{T,u"𝐋"}
const Met{T} = Quantity{T,u"𝐋",typeof(m)}
const Deg{T} = Quantity{T,NoDims,typeof(°)}
const Rad{T} = Quantity{T,NoDims,typeof(rad)}

```

You don’t need to construct concrete Unitful.jl types for dispatch. And you can let Julia figure out the exact type in a field using a type parameter:

```julia
struct Foo{L<:Met}
  len::L
end

```
