# Understanding generic functions and parametric types

**URL:** <https://discourse.julialang.org/t/understanding-generic-functions-and-parametric-types/117810>\
**Category:** New to Julia\
**Tags:** parametric-types, functions\
**Created:** [August 4, 2024, 3:13pm UTC](https://discourse.julialang.org/t/understanding-generic-functions-and-parametric-types/117810 "2024-08-04T15:13:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oblomov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oblomov/32/211073_2.png) [@Oblomov](https://discourse.julialang.org/u/Oblomov)\
**Post date:** [August 4, 2024, 3:13pm UTC](https://discourse.julialang.org/t/understanding-generic-functions-and-parametric-types/117810/1 "2024-08-04T15:13:00Z")

</div>

Hello all,

I’m new to Julia and I’m trying to understand how type deduction works for generic functions with parametric types. To test my hands at things, I tried to define a function that takes one argument that should be a vector-like of tuples with two elements, a numeric matrix-like and a string. So I tried the following:

```
f(::AbstractVector{Tuple{AbstractMatrix{T},String}}) where T<:Number = @show T

```

I then defined a matrix `a`

```
a = [11 12 13 ; 21 22 23]

```

and expected `f( [(a, "a")] )` to be equivalent to `@show Int64`, but instead got the error:

```julia
julia> f( [(a, "a")])
ERROR: MethodError: no method matching f(::Vector{Tuple{Matrix{Int64}, String}})

Closest candidates are:
  f(::AbstractArray{Tuple{AbstractMatrix{T}, String}, 1}) where T<:Number

```

So what am I missing?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [August 4, 2024, 3:19pm UTC](https://discourse.julialang.org/t/understanding-generic-functions-and-parametric-types/117810/2 "2024-08-04T15:19:29Z")

</div>

Hi there! If we take things step by step to check subtyping, we’ll see what went wrong:

```julia
julia> a isa AbstractMatrix
true

julia> (a, "a") isa Tuple{AbstractMatrix, String}
true

julia> [(a, "a")] isa AbstractArray{Tuple{AbstractMatrix, String}}
false

julia> [(a, "a")] isa AbstractArray{<:Tuple{AbstractMatrix, String}}
true

```

The problem here is that just because two types satisfy `T1 <: T2` doesn’t mean `AbstractArray{T1} <: AbstractArray{T2}`. The section of the docs explaining this is [Parametric Composite Types](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types).  
Note that the behavior of `Tuple` is [different](https://docs.julialang.org/en/v1/manual/types/#Tuple-Types) from that of other parametric types, which is something I always struggle to remember.

---

<div class="post-metadata">

**Author:** ![Oblomov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oblomov/32/211073_2.png) [@Oblomov](https://discourse.julialang.org/u/Oblomov)\
**Post date:** [August 4, 2024, 3:41pm UTC](https://discourse.julialang.org/t/understanding-generic-functions-and-parametric-types/117810/3 "2024-08-04T15:41:06Z")

</div>

Thank you very much!

I had already gone through the [Types](https://docs.julialang.org/en/v1/manual/types/) and [More about types](https://docs.julialang.org/en/v1/devdocs/types/) pages in the docs, but it’s a lot to digest, so I missed that particular aspect. The fact that when testing with tuples it hadn’t tripped didn’t help indeed 😉

I’ve gone with `AbstractVector{Tuple{M,String}} where M <: AbstractMatrix{<:Number}`, but now I’ve got a related problem for “diagonal types”, if my understanding is correct. I’ll open a new topic for it. Thanks again.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [August 4, 2024, 3:58pm UTC](https://discourse.julialang.org/t/understanding-generic-functions-and-parametric-types/117810/4 "2024-08-04T15:58:15Z")

</div>

You might also find the in-development version of the [`<:`](https://docs.julialang.org/en/v1.12-dev/base/base/#Core.:%3C:) doc string useful.
