# Dispatch on both parametrized and non-parametrized type

**URL:** <https://discourse.julialang.org/t/dispatch-on-both-parametrized-and-non-parametrized-type/1246>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 2, 2017, 2:15pm UTC](https://discourse.julialang.org/t/dispatch-on-both-parametrized-and-non-parametrized-type/1246 "2017-01-02T14:15:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 2, 2017, 2:15pm UTC](https://discourse.julialang.org/t/dispatch-on-both-parametrized-and-non-parametrized-type/1246/1 "2017-01-02T14:15:13Z")

</div>

I would like to write a function that returns something for each type it is defined for. Importantly,

1. it needs to dispatch on **types** , not instances, since I need the result before I create the latter,
2. it would be nice if it worked on parametric types, giving the same result regardless of the parameters.

An MWE is

```julia
immutable Foo{T} x::T end

transformation(::Type{Foo}) = :standard
transformation{T}(::Type{Foo{T}}) = :standard

```

and my problem is that I have to define both for `Foo` and `Foo{T}` because of invariance (if I understand correctly). This becomes cumbersome if the type has more parameters.

Is there a way to define it once, and have it work for `Foo` and eg `Foo{Float64}`?

---

<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:** [January 2, 2017, 2:45pm UTC](https://discourse.julialang.org/t/dispatch-on-both-parametrized-and-non-parametrized-type/1246/2 "2017-01-02T14:45:33Z")

</div>

```julia
transformation{T<:Foo}(::Type{T}) = :standard

```

?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 2, 2017, 3:05pm UTC](https://discourse.julialang.org/t/dispatch-on-both-parametrized-and-non-parametrized-type/1246/3 "2017-01-02T15:05:45Z")

</div>

Thanks, I overlooked this.

Can someone explain why

```julia
julia> immutable Foo{T} x::T end

julia> Foo <: Foo{Int}
false

julia> Foo{Int} <: Foo
true

```

but

```julia
julia> supertype(Foo{Int})
Any

```

that is to say, if `A <: B` and `!(B <: A)`, I would expect to find `B` among the supertypes of `A` somewhere along the chain. Apparently I have the wrong intuition.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 3, 2017, 6:02pm UTC](https://discourse.julialang.org/t/dispatch-on-both-parametrized-and-non-parametrized-type/1246/4 "2017-01-03T18:02:16Z")

</div>

> that is to say, if A \<: B and !(B \<: A), I would expect to find B among the supertypes of A somewhere along the chain. Apparently I have the wrong intuition.

There’s an infinite number of supertypes. The one returned by `supertype` is simply one choice. Consider, for example, what the shape of the type-tree of a parameterized type must look like:

```julia
julia> immutable Foo{T} <: Abstract{T} end

```

Here we have that `Foo{T} <: Abstract{T}`, `Foo{T} <: Foo`, `Foo <: Abstract`, and `Abstract{T} <: Abstract`. All of these are supertypes, but you cannot arrange them linearly.  
To get the infinite set, you have to add in the consideration that a larger type can also be formed by taking the union of the type with any other type, or by widening any of the type-variables.

The supertype `Foo{T} <: Foo` would be formed by widening the type-variables from `T` to (one of) its supertype. The `supertype` function is instead formed as a widening of the base type.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 3, 2017, 6:32pm UTC](https://discourse.julialang.org/t/dispatch-on-both-parametrized-and-non-parametrized-type/1246/5 "2017-01-03T18:32:19Z")

</div>

> [@jameson](#):
>
> There’s an infinite number of supertypes.

Thanks, the example was really enlightening.
