# Specify type when calling parametric function

**URL:** <https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805>\
**Category:** General Usage\
**Created:** [September 16, 2019, 6:02am UTC](https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805 "2019-09-16T06:02:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [September 16, 2019, 6:02am UTC](https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805/1 "2019-09-16T06:02:16Z")

</div>

Say I have a parametric function:

```julia
foo(a::Union{T, Vector{T}}) where {T} = T

```

Calling `foo(ones(1))` returns `Float64`, implying that Julia has decided that `a` was of type `Vector{T}`. Is it possible to call this function explicitly specifying `T` as `Vector{Float64}`?

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [September 16, 2019, 6:25am UTC](https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805/2 "2019-09-16T06:25:43Z")

</div>

> Calling `foo(ones(1))` returns `Float64` , implying that Julia has decided that `a` was of type `Vector{T}` .

Well, it is, so that’s not too surprising 😉

But I’m not really sure what you want to know?

```julia
bar(a::T) where {T} = T

```

returns `Array{Float64,1}` (`== Vector{Float64}`) for `bar(ones(1))`, are you looking for that?

---

<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:** [September 16, 2019, 6:30am UTC](https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805/3 "2019-09-16T06:30:40Z")

</div>

> [@aahaselgrove](#):
>
> Is it possible to call this function explicitly specifying `T` as `Vector{Float64}` ?

Not sure what you are asking about here, but

```julia
julia> foo([[1.0]])
Array{Float64,1}

```

which is a `Vector{Float64}`.

---

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [September 16, 2019, 6:35am UTC](https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805/4 "2019-09-16T06:35:22Z")

</div>

[https://github.com/wildart/Evolutionary.jl/blob/c7fb224c7d17691aaff0f5ce63987df9f3329c97/src/ga.jl#L46-L58](https://github.com/wildart/Evolutionary.jl/blob/c7fb224c7d17691aaff0f5ce63987df9f3329c97/src/ga.jl#L46-L58)

I’m trying to make this code more generic to allow for multidimensional individuals, with the function signature

```julia
function ga(objfun::Function, dims::Tuple;
            initPopulation::Union{T, Vector{T}, Function} = trues(dims),
            lowerBounds::Union{Nothing, Vector{T}} = nothing,
            upperBounds::Union{Nothing, Vector{T}} = nothing,
            populationSize::Int = 50,
            crossoverRate::Float64 = 0.8,
            mutationRate::Float64 = 0.1,
            ɛ::Real = 0,
            selection::Function = ((x,n)->1:n),
            crossover::Function = ((x,y)->(y,x)),
            mutation::Function = (x->x),
            iterations::Integer = 100*prod(dims),
            tol = 0.0,
            tolIter = 10,
            verbose = false,
            debug = false,
            interim = false) where {T}

```

The author currently prescribes different behaviour based on whether or not the `initPopulation` parameter is the same type as an individual or is a dimension higher, and I am trying to replicate this (hence, the `Union{T, Vector{T}}` in my original response). It would be easy to infer what T should be if Julia Function types could be parameterised by inputs/outputs (because the `objfun` parameter is a function which takes a `T` object and returns a float), but I don’t believe this can be done. If I provided a `lowerBounds` or `upperBounds` parameter, it would be easy to infer what `T` should be as this parameter only accepts `Vector{T}`, but I don’t want to do this. Hence, I would like to explicity specify `T` when calling the `ga` function, but I cannot figure out how to do this (I am guessing it’s not possible).

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 16, 2019, 6:59am UTC](https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805/5 "2019-09-16T06:59:55Z")

</div>

Maybe

```julia
foo(a::Union{T, Vector{T}}) where T::Vector{Float64}

```

---

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [September 16, 2019, 7:12am UTC](https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805/6 "2019-09-16T07:12:49Z")

</div>

The point is that `T` is supposed to be flexible. The `Vector{Float64}` from the original post was just an example.

---

<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:** [September 16, 2019, 7:33am UTC](https://discourse.julialang.org/t/specify-type-when-calling-parametric-function/28805/7 "2019-09-16T07:33:33Z")

</div>

Generally, if you are making a PR to a package, it is best to ask the package authors/maintainers about their preferred style.

That said, rather than go about this in the rather roundabout way you suggest, I would just make `initial_population` a parameter and let the caller initialize it as they please.
