# Dispatch on type aliases?

**URL:** https://discourse.julialang.org/t/dispatch-on-type-aliases/70691
**Category:** Internals & Design
**Tags:** type, inheritance, dispatch
**Created:** [October 31, 2021, 3:13pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691 "2021-10-31T15:13:01Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [October 31, 2021, 3:13pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691/1 "2021-10-31T15:13:02Z")

</div>

Can Julia have dispatch based on type aliases? Something rather similar to Scala 3’s [opaque type aliases](https://docs.scala-lang.org/scala3/book/types-opaque-types.html).

```julia
const MyTypeAlias = Float64
a::MyTypeAlias = 78.0 # The compiler should implicitly convert this from Float64 to MyTypeAlias

some_func(a::Float64) = :foo
some_func(a::MyTypeAlias) = :bar
other_func(a::Float64) = :hi

@assert some_func(78.0) == :foo
@assert some_func(a) == :bar
@assert other_func(a) == :hi # MyTypeAlias should be a subtype of Float64

```

This is rather similar to concrete single inheritance, which I have heard will cause problems for the compiler; But in this case, as the types are the same and we are just changing the dispatch, this should be possible?

PS: I have used `Float64` just as an example, please don’t suggest specific workarounds for numeric types.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 31, 2021, 3:17pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691/2 "2021-10-31T15:17:03Z")

</div>

no. type aliases are just names the compiler doesn’t even know they exist.

---

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [October 31, 2021, 3:18pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691/3 "2021-10-31T15:18:27Z")

</div>

I know that this currently doesn’t work, I am asking if adding this has any technical difficulty. It’s a neat feature to have. It should probably use a new keyword like `typealias foo = bar` if it is to be added.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 31, 2021, 3:22pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691/4 "2021-10-31T15:22:08Z")

</div>

I think it is very unlikely that we would add it. I know it seems like a reasonable idea, but it doesn’t work. the biggest problem is you would be subtyping concrete types which is really bad (ie would break absolutely everything)

---

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [October 31, 2021, 4:19pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691/5 "2021-10-31T16:19:19Z")

</div>

Can you elucidate on why this would break the compiler? Since the types are exactly the same, the memory layout shouldn’t be a problem. Will the inlining break? (I am just trying to get a rudimentary sense of why this isn’t possible, not trying to argue that it is.)

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 31, 2021, 4:38pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691/6 "2021-10-31T16:38:15Z")

</div>

What should the following code do?

```julia
typealias Int1 = Int
typealias Int2 = Int
x=Int1(1)
y=Int2(1)
x+y

```

what about

```julia
typealias Int3 = Int1
z=Int3(1)
y+z

```

---

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [October 31, 2021, 5:22pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691/7 "2021-10-31T17:22:02Z")

</div>

I don’t see any ambiguity here? The method with the narrowest type matches should be used, which would be sth along the lines of `+(x::Int, y::Int)` for all three aliases as they don’t define any `+` methods of their own.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 31, 2021, 5:23pm UTC](https://discourse.julialang.org/t/dispatch-on-type-aliases/70691/8 "2021-10-31T17:23:12Z")

</div>

See this issue [https://github.com/JuliaLang/julia/issues/9821](https://github.com/JuliaLang/julia/issues/9821) for discussion about this.
