# Type changes based on declaration

**URL:** <https://discourse.julialang.org/t/type-changes-based-on-declaration/35604>\
**Category:** New to Julia\
**Created:** [March 5, 2020, 7:39pm UTC](https://discourse.julialang.org/t/type-changes-based-on-declaration/35604 "2020-03-05T19:39:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)\
**Post date:** [March 5, 2020, 7:39pm UTC](https://discourse.julialang.org/t/type-changes-based-on-declaration/35604/1 "2020-03-05T19:39:21Z")

</div>

Why aren’t F and V below of the same type? How can I write a function that will handle both of them (but not also any generic vector)?

```julia
using SparseArrays

F = Vector{SparseVector}()
push!(F, sparse([1, -1, 5, -5]))
push!(F, sparse([4, -4, -3, 3]))
push!(F, sparse([3, -3, 1, -4]))
push!(F, sparse([8, -8, 1, -1]))

V = [
    sparse([1, -1, 5, -5]),
    sparse([4, -4, -3, 3]),
    sparse([3, -3, 1, -4]),
    sparse([8, -8, 1, -1])
]

typeof(F) # Vector{SparseVector}
typeof(V) # Vector{SparseVector{Int64, Int64}}

```

So, for example, this function

```julia
function foo(V::Vector{SparseVector})
    println("My function 'tis of thee.")
end

```

only works on `F`, and this function

```julia
function foo(V::Vector{SparseVector{Int,Int}})
    println("Sweet Int of Int I see.")
end

```

only works on `V`. I’d like to write a function that will work on both.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 5, 2020, 7:43pm UTC](https://discourse.julialang.org/t/type-changes-based-on-declaration/35604/2 "2020-03-05T19:43:12Z")

</div>

> [@squirrel](#):
>
> I’d like to write a function that will work on both.

```julia
function foo(V::Vector{<:SparseVector})
    println("My function 'tis of thee.")
end

```

It’s called “invariance”, have a search through the manual to find out what it is.

---

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)\
**Post date:** [March 5, 2020, 8:56pm UTC](https://discourse.julialang.org/t/type-changes-based-on-declaration/35604/3 "2020-03-05T20:56:59Z")

</div>

Ah! I was so close… I tried

```julia
function foo(v<:Vector{SparseVector})

```

and

```julia
function foo(v::T) where T<:Vector{SparseVector}

```

but now I see why those were wrong. THANKS!

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 5, 2020, 9:10pm UTC](https://discourse.julialang.org/t/type-changes-based-on-declaration/35604/4 "2020-03-05T21:10:12Z")

</div>

> [@squirrel](#):
>
> Why aren’t F and V below of the same type?

Not sure if this maters anymore, but when you declared F you **made** if type `Vector{SparseVector}()` but when you declared V you let Julia figure out the type, so it was able to identify the types in the SparseVector object.

So for F you could have done:

```julia
F = Vector{SparseVector{Int64, Int64}}()

```

And both would be the same…

---

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)\
**Post date:** [March 5, 2020, 9:29pm UTC](https://discourse.julialang.org/t/type-changes-based-on-declaration/35604/5 "2020-03-05T21:29:10Z")

</div>

Thanks! I figured it was something like that, but when I was playing around with `Vector{SparseVector{Any,Int}}` and things like that,

```julia
Vector{SparseVector{Int,Int}} <: Vector{SparseVector}
Vector{SparseVector{Int,Int}} <: Vector{SparseVector{Any,Int}}

```

are both false, and I couldn’t quite figure out how to make it work. The placement of `<:` is what I needed.
