# Dispatch Question

**URL:** <https://discourse.julialang.org/t/dispatch-question/47826>\
**Category:** General Usage\
**Created:** [October 6, 2020, 2:57am UTC](https://discourse.julialang.org/t/dispatch-question/47826 "2020-10-06T02:57:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [October 6, 2020, 2:57am UTC](https://discourse.julialang.org/t/dispatch-question/47826/1 "2020-10-06T02:57:42Z")

</div>

```julia
abstract type Def{T} end

struct YY <: Def{Number}
end

f1(x::Def{<:T}) where T = T
f2(x::Def{T}) where T = T

julia> f1(YY())
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] f1(::YY) at ./REPL[3]:1
 [2] top-level scope at REPL[5]:1

julia> f2(YY())
Number

```

Why f1(YY()) doesn’t work?

But the following works:

```julia
julia> struct Sym{T}
       end

julia> f3(x::Sym{<:T}) where T = T
f3 (generic function with 1 method)

julia> f4(x::Sym{T}) where T = T
f4 (generic function with 1 method)

julia> f3(Sym{Float64}())
Float64

julia> f4(Sym{Float64}())
Float64

```

---

<div class="post-metadata">

**Author:** ![tisztamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tisztamo/32/16200_2.png) [@tisztamo](https://discourse.julialang.org/u/tisztamo)\
**Post date:** [October 6, 2020, 6:08am UTC](https://discourse.julialang.org/t/dispatch-question/47826/2 "2020-10-06T06:08:07Z")

</div>

No answer here, but interested: what are f1 and f3 for?

Also:

```julia
julia> f3(Sym{Number}())
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] f3(::Sym{Number}) at ./REPL[2]:1
 [2] top-level scope at REPL[5]:1

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 6, 2020, 9:52am UTC](https://discourse.julialang.org/t/dispatch-question/47826/3 "2020-10-06T09:52:39Z")

</div>

Actually, they work the same:

```julia
julia> f3(Sym{Number}())
ERROR: UndefVarError: T not defined

```

In the definitions of `f1` and `f3`, we have something like: “the parameter of the input type is a subtype of T. Return T”. No further information is given about what T is or could be, so what should be returned? You could argue that the answer is `Any`, but I think it makes more sense to interpret this as an error, which is what’s happening. `f2` and `f4` make sense (“return the parameter of the input’s type”).

As a sidenote, for `Def` and `YY`, you probably meant (although I could be wrong)

```julia
struct YY{T<:Number} <: Def{T} end

```

I.e. keeping the type parameter on YY, but making sure it is always a subtype of a `Number`. By restricting to an abstract type like you were doing (i.e. `Def{Number}` rather than `Def{<:Number}`) you _might_ be losing some performance and/or flexibility in your application of this type that parameterized types can give.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [October 6, 2020, 5:37pm UTC](https://discourse.julialang.org/t/dispatch-question/47826/4 "2020-10-06T17:37:26Z")

</div>

> [@tomerarnon](#):
>
> ```julia
> julia> f3(Sym{Number}())
> ERROR: UndefVarError: T not defined
> 
> ```

It works on my Julia. And it still doesn’t answer my question, why it works on a struct (concrete type) but doesn’t work on a abstract type (Number)?

Thanks

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [October 6, 2020, 5:48pm UTC](https://discourse.julialang.org/t/dispatch-question/47826/5 "2020-10-06T17:48:35Z")

</div>

> [@Qiyamah](#):
>
> ```julia
> f1(x::Def{<:T}) where T = T
> 
> ```

I think that’s not really well defined. You’re asking for a type which is a supertype of `Number`. `T=Number` is an answer, but so is `T=Any`. I’m a bit surprised that it’s not an error, but maybe it’s hard to circumscribe when to throw the error.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 6, 2020, 9:02pm UTC](https://discourse.julialang.org/t/dispatch-question/47826/6 "2020-10-06T21:02:52Z")

</div>

Honestly, I don’t know why `f3(Sym{Float64}())` works. As far as I can tell, it’s equally nonsensical…

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [October 7, 2020, 9:01am UTC](https://discourse.julialang.org/t/dispatch-question/47826/7 "2020-10-07T09:01:25Z")

</div>

Agreed, seems like a valid issue to post / look for on the julia github.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [October 7, 2020, 6:22pm UTC](https://discourse.julialang.org/t/dispatch-question/47826/8 "2020-10-07T18:22:08Z")

</div>

It is not nonsensical it does what it does it returns you the least upper bound. Which comes handy in dealing with covariant types. But the issue occurs when T is abstract type.
