# Julia automatically converts datatype in function call

**URL:** <https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756>\
**Category:** New to Julia\
**Created:** [July 8, 2020, 10:03pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756 "2020-07-08T22:03:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogeriog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogeriog/32/16255_2.png) [@rogeriog](https://discourse.julialang.org/u/rogeriog)\
**Post date:** [July 8, 2020, 10:03pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/1 "2020-07-08T22:03:50Z")

</div>

I have a function that takes Int32 values as input.

```julia
function overlap(alpha1::Float64, l1::Int32, m1::Int32, n1::Int32, a::Vector{Float64},
    alpha2::Float64, l2::Int32, m2::Int32, n2::Int32, b::Vector{Float64})
    ---implementation ---
    return value;
end

```

Now I want to calculate for a set of variables:  
`overlap(gto1.alpha, gto1.l, gto1.m, gto1.n, gto1.r, gto2.alpha, gto2.l+2, gto2.m, gto2.n, gto2.r)`  
However, the value with addition return a Int64 and therefore causes an error. I have to do:  
`overlap(gto1.alpha, gto1.l, gto1.m, gto1.n, gto1.r, gto2.alpha, Int32(gto2.l+2), gto2.m, gto2.n, gto2.r)`  
For it to work correctly. Is there a way to make Julia not convert the datatypes when it operates on them like this? I am doing this kind of correction many times in my code.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [July 8, 2020, 10:08pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/2 "2020-07-08T22:08:28Z")

</div>

What exactly are you doing? Adding two `Int32` gives an `Int32`:

```julia
julia> Int32(1) + Int32(2)
3

julia> typeof(Int32(1) + Int32(2))
Int32

```

Is the problem that you’re using literals that have type `Int64` like `2`?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 8, 2020, 10:18pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/3 "2020-07-08T22:18:19Z")

</div>

The general solution is to not use such tight type annotations. It’s rare that you actually need to restrict a particular implementation to only accepting `::Int32`s unless you’re doing really low-level bit banging. Simply using `::Integer` would work just as well. Heck, I’d probably not worry about _any_ types in your method definition at all.

Julia’s performance does not change with the addition or removal of type annotations on a method’s definition.

---

<div class="post-metadata">

**Author:** ![ppalmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ppalmes/32/6838_2.png) [@ppalmes](https://discourse.julialang.org/u/ppalmes)\
**Post date:** [July 8, 2020, 10:21pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/4 "2020-07-08T22:21:42Z")

</div>

use this function signature

`function overlap(a::T, b::T) where {T<:Int32} .... end`  
to make sure both arguments are Int32 and then it will satisfy closure property.

---

<div class="post-metadata">

**Author:** ![rogeriog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogeriog/32/16255_2.png) [@rogeriog](https://discourse.julialang.org/u/rogeriog)\
**Post date:** [July 8, 2020, 10:48pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/5 "2020-07-08T22:48:24Z")

</div>

Yes the problem was the +2.

---

<div class="post-metadata">

**Author:** ![rogeriog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogeriog/32/16255_2.png) [@rogeriog](https://discourse.julialang.org/u/rogeriog)\
**Post date:** [July 8, 2020, 10:50pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/6 "2020-07-08T22:50:35Z")

</div>

It is nice to know that.

---

<div class="post-metadata">

**Author:** ![rogeriog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rogeriog/32/16255_2.png) [@rogeriog](https://discourse.julialang.org/u/rogeriog)\
**Post date:** [July 8, 2020, 10:58pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/7 "2020-07-08T22:58:39Z")

</div>

It doesnt solve the addition conversion problem when the function is called but it’s definitely easier to declare this way.

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [July 8, 2020, 11:23pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/8 "2020-07-08T23:23:47Z")

</div>

I wouldn’t call it a “conversion problem” since `2` is an `Int64` and the most sensible thing when adding an `Int32` and `Int64` is to return an `Int64`. I think there have been discussions elsewhere in this forum about making numeric literals default to a different type. But as noted above, the best solution is to not be so restrictive on your arguments.

---

<div class="post-metadata">

**Author:** ![ppalmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ppalmes/32/6838_2.png) [@ppalmes](https://discourse.julialang.org/u/ppalmes)\
**Post date:** [July 8, 2020, 11:39pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/9 "2020-07-08T23:39:33Z")

</div>

+2 by default is int64. you should use gto2.l+Int32(2)

---

<div class="post-metadata">

**Author:** ![ppalmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ppalmes/32/6838_2.png) [@ppalmes](https://discourse.julialang.org/u/ppalmes)\
**Post date:** [July 8, 2020, 11:46pm UTC](https://discourse.julialang.org/t/julia-automatically-converts-datatype-in-function-call/42756/10 "2020-07-08T23:46:25Z")

</div>

you should better pass the structure into the function:  
`function overlap(gto1::MyStructure, gto2::MyStructure, l::Int32)`  
…  
end
