# Implement a function for Vectors of all subtypes

**URL:** <https://discourse.julialang.org/t/implement-a-function-for-vectors-of-all-subtypes/630>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 29, 2016, 2:43pm UTC](https://discourse.julialang.org/t/implement-a-function-for-vectors-of-all-subtypes/630 "2016-11-29T14:43:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [November 29, 2016, 2:43pm UTC](https://discourse.julialang.org/t/implement-a-function-for-vectors-of-all-subtypes/630/1 "2016-11-29T14:43:13Z")

</div>

I have a problem i can’t find a solution for.  
Lets say I have a abstract supertype

```
abstract A

```

and a subtype

```
type B <: A
  value::Vector
end

```

(others might want to have `value` e.g. to be a matrix or something)

And I implemented a few functions for each subtype, maybe `f1(p::B)::Float64` (and for other subtypes, too…) and now I’d like to implement a function such as

```
function myVFun(p::Vector{A},lambda::Integer)
  sum( [f1(v) for v in p] )
  # but really this is of course more complicated involving 2-3 functions all available for each subtype of A
  return ...
end

```

Somehow the method dispatch does not call this function if I now have a `Vector{B}` (because this is not a subtype of `Vector{A}`?). But however I qould like to be able to implement (and call) such a function using vectors of subtypes. Because all functions I apply to the elements of `p::Vector{A}` are available for `B <: A`…

How can I do that?

---

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [November 29, 2016, 2:52pm UTC](https://discourse.julialang.org/t/implement-a-function-for-vectors-of-all-subtypes/630/2 "2016-11-29T14:52:56Z")

</div>

Vector{B} is not a sub-type of vector{A}

from the docs:

> Even though Float64 \<: Real we DO NOT have Point{Float64} \<: Point{Real}.  
> In other words, in the parlance of type theory, Julia’s type parameters are invariant, rather than being covariant (or even contravariant).

what you are looking for is something along these lines:

```julia
function myVFun{T <: A}(p::Vector{T},lambda::Integer)
  return sum( [f1(v) for v in p] )
end

```

Basically you are defining a parametric function for every type that is a sub-type of A

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [November 29, 2016, 3:09pm UTC](https://discourse.julialang.org/t/implement-a-function-for-vectors-of-all-subtypes/630/3 "2016-11-29T15:09:54Z")

</div>

And I can call the function then with `myVFun(f)`, when `f::Vector{B}`?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 29, 2016, 3:13pm UTC](https://discourse.julialang.org/t/implement-a-function-for-vectors-of-all-subtypes/630/4 "2016-11-29T15:13:56Z")

</div>

Use a parameterized type this way:

```julia
import Base: length

abstract A{T}
type B{T} <: A{T}
   value::Vector{T}
end

length{T}(x::B{T}) = length(x.value)

function myFunction{T}(x::B{T})
    n = length(x)
    s = "A vector of type Vector{$T} and length $n."
    return s
end

myInts = B( [1,2,3] )
myChars = B( ['a', 'b', 'c', 'd'] )

myFunction(myInts)
myFunction(myChars)

```

Running that displays

```julia
julia> myInts = B( [1,2,3] )
B{Int64}([1,2,3])

julia> myChars = B( ['a', 'b', 'c'] )
B{Char}(['a','b','c'])

julia> myFunction(myInts)
"A vector of type Vector{Int64} and length 3."

julia> myFunction(myChars)
"A vector of type Vector{Char} and length 4."

```

Do you questions about how this example works?

You can have specialized handling for vectors of a given type:

```julia
# specialize the handling of Bs with Integer vectors
function myFunction{ T<:Integer }(x::B{T})
    s = "Specialized Handling for Bs with Integer vectors"
    return s
end

```

now running the example displays

```julia
julia> myFunction(myChars)
"A vector of type Vector{Char} and length 3."

julia> myFunction(myInts)
"Specialized Handling for Bs with Integer vectors"

```

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [November 29, 2016, 3:45pm UTC](https://discourse.julialang.org/t/implement-a-function-for-vectors-of-all-subtypes/630/5 "2016-11-29T15:45:50Z")

</div>

Thanks @jsarnoff your example made the point very clear and even the specialization is neatly explained. The method dispatching now works.

Edit: And it even works for operator overloading in order to use the subtype constructor

```julia
+{B <: A}(a::T,b::T) = T(a.value+b.value)

```

thats really neat!
