# Confused about parametric method definitions

**URL:** <https://discourse.julialang.org/t/confused-about-parametric-method-definitions/3898>\
**Category:** New to Julia\
**Created:** [May 24, 2017, 5:19am UTC](https://discourse.julialang.org/t/confused-about-parametric-method-definitions/3898 "2017-05-24T05:19:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![metjmart](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@metjmart](https://discourse.julialang.org/u/metjmart)\
**Post date:** [May 24, 2017, 5:19am UTC](https://discourse.julialang.org/t/confused-about-parametric-method-definitions/3898/1 "2017-05-24T05:19:20Z")

</div>

As a beginner programmer who’s relatively new to Julia, I’m a bit confused about how method definitions work. If I define a simple function as follows:

```julia
function xy{T<:Real}(x::Vector{T}, y::Vector{T})
    out = x .* y
end

```

Why do I get a `MethodError` if, for example, `x` is of type `Int64` and `y` is of type `Float64`? There’s a similar example in the Methods documentation, but it’s still not clear to me why both types can’t be used in the function if they are both subtypes of `Real`. This may stem from my misunderstanding of how type parameters are used in the method signature.

I often work with data that can be of various types and it would be rather cumbersome to convert them all to similar types prior to executing a function. For example, the function above wouldn’t work if `x` was of type `Float32` and y was of type `Float64`. A trivial solution would be a more generalized form of the function:

```julia
function xy(x::Vector, y::Vector)
    out = x .* y
end

```

However, I feel that this isn’t ideal. If I wanted to add a two-dimensional array to the parameter tuple, I’m not sure how I would explicitly enforce the dimensionality of the array without the presence of a method type parameter `T`. For example, a function with definition as follows:

```julia
function xyz{T<:Real}(x::Vector{T}, y::Vector{T}, z::Array{T,2})

```

would require `z` to be a 2-dimensional array, but also require it have the same type as `x` and `y`.

I was hoping someone could provide me with some clarity on the technical aspects involved with this. And more importantly, what is the recommended “Julian” way of proceeding if `x`, `y`, and `z` are of different types?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 24, 2017, 5:41am UTC](https://discourse.julialang.org/t/confused-about-parametric-method-definitions/3898/2 "2017-05-24T05:41:21Z")

</div>

> [@metjmart](#):
>
> Why do I get a MethodError if, for example, x is of type Int64 and y is of type Float64? There’s a similar example in the Methods documentation, but it’s still not clear to me why both types can’t be used in the function if they are both subtypes of Real. This may stem from my misunderstanding of how type parameters are used in the method signature.

What you wrote there is “both must be vectors of type `T`, where `T` must be some `Real` number”. That’s different then

```julia
function xy{T<:Real,T2<:Real}(x::Vector{T}, y::Vector{T2})
    out = x .* y
end

```

Also note the shorthand

```julia
function xy(x::Vector{<:Real}, y::Vector{<:Real})
    out = x .* y
end

```

for v0.6.

> [@metjmart](#):
>
> I’m not sure how I would explicitly enforce the dimensionality of the array without the presence of a method type parameter T

You can always do `ndims(x)`, or use `typeof(x)` or `eltype(x)` in the function.

In this sense, why would you restrict to `Vector` and `Array` instead of `AbstractVector` and `AbstractArray`? I cannot think of many examples which actually need a standard array (those that do, it’s because they are calling C).

---

<div class="post-metadata">

**Author:** ![metjmart](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@metjmart](https://discourse.julialang.org/u/metjmart)\
**Post date:** [May 24, 2017, 3:18pm UTC](https://discourse.julialang.org/t/confused-about-parametric-method-definitions/3898/3 "2017-05-24T15:18:07Z")

</div>

> [@ChrisRackauckas](#):
>
> What you wrote there is “both must be vectors of type T, where T must be some Real number”. That’s different then
> 
> function xy{T\<:Real,T2\<:Real}(x::Vector{T}, y::Vector{T2})  
> out = x .\* y  
> end

Ok, this makes more sense. I guess I wasn’t properly following the logic here.

> [@ChrisRackauckas](#):
>
> Also note the shorthand
> 
> function xy(x::Vector{\<:Real}, y::Vector{\<:Real})  
> out = x .\* y  
> end
> 
> for v0.6.

This looks like a much better alternative to all the previous solutions. Thanks for the help!
