# Using a type parameter vs abstract type in function signature

**URL:** https://discourse.julialang.org/t/using-a-type-parameter-vs-abstract-type-in-function-signature/112339
**Category:** General Usage
**Tags:** type, parametric-types
**Created:** [March 30, 2024, 10:41pm UTC](https://discourse.julialang.org/t/using-a-type-parameter-vs-abstract-type-in-function-signature/112339 "2024-03-30T22:41:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mdsalerno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsalerno/32/5539_2.png) [@mdsalerno](https://discourse.julialang.org/u/mdsalerno)
#### Post date: [March 30, 2024, 10:41pm UTC](https://discourse.julialang.org/t/using-a-type-parameter-vs-abstract-type-in-function-signature/112339/1 "2024-03-30T22:41:15Z")

</div>

I’ve written the following function:

```julia
function all_your_base(digits::Vector{Integer}, base_in, base_out) 
    sum(digits) ≠ 0 || return([0])
    n = digits .* base_in .^ (length(digits)-1:-1:0) |> sum
    res = []
    for d ∈ base_out .^ (floor(log(base_out, n)):-1:0)
        push!(res, n ÷ d)
        n -= res[end] * d
    end
    res
end

```

This fails (MethodError) if I call it as follows:

```julia
all_your_base([1], 2, 10)

```

However it works if I rewrite my function using a type parameter:

```julia
function all_your_base(digits::Vector{T}, base_in, base_out) where {T<:Integer}
    sum(digits) ≠ 0 || return([0])
    n = digits .* base_in .^ (length(digits)-1:-1:0) |> sum
    res = []
    for d ∈ base_out .^ (floor(log(base_out, n)):-1:0)
        push!(res, n ÷ d)
        n -= res[end] * d
    end
    res
end

```

I don’t understand why the two behave differently. I thought these functions identical, except for the fact that using the type parameter gives me access to “T” within the body of the function.

---

<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: [March 30, 2024, 11:30pm UTC](https://discourse.julialang.org/t/using-a-type-parameter-vs-abstract-type-in-function-signature/112339/2 "2024-03-30T23:30:37Z")

</div>

you want `Vector{<:Integer}`. The term to explain why is “type invariance”.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 31, 2024, 12:18am UTC](https://discourse.julialang.org/t/using-a-type-parameter-vs-abstract-type-in-function-signature/112339/3 "2024-03-31T00:18:52Z")

</div>

Or, as I understand it: [Vector{Int} \<: Vector{Real} is false??? · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/typevariance/)
