# Do I need to specify concrete type of a variable such as Int64 or just Int, Float64 or just Float?

**URL:** <https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047>\
**Category:** General Usage\
**Created:** [August 8, 2021, 9:53pm UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047 "2021-08-08T21:53:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 8, 2021, 9:53pm UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/1 "2021-08-08T21:53:04Z")

</div>

In Julia, do I need to really set very specific and concrete type of the variable?  
Like,

```
i::Int64

```

or

```
i::Int

```

and

```
x::Float

```

or

```
x::Float64

```

Is there performance difference?  
Or, like, is it just using Int and Float is enough, and no need to particular do Int64 and Float64?

Thanks!

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 8, 2021, 10:01pm UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/2 "2021-08-08T22:01:52Z")

</div>

On a 64-bit platform, `Int` is just another name for `Int64`. There is no `Float`. However, often the types can be inferred and don’t need to be annotated at all.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 9, 2021, 12:16am UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/3 "2021-08-09T00:16:03Z")

</div>

If I want to enforce say integers in a function call, is it better to restrict the types on which the function can be called, or to throw an particular error:  
option 1:

```julia
function f1(n::Integer) = n + 1

```

option 2:

```julia
function f2(n)
    typeof(n) <: Integer || throw(error("error"))
    n + 1
end

```

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 9, 2021, 12:31am UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/4 "2021-08-09T00:31:15Z")

</div>

@amrods It is better to use the type parameter in the signature.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 9, 2021, 12:47am UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/5 "2021-08-09T00:47:56Z")

</div>

> [@CRquantum](#):
>
> Is there performance difference?

Almost never, and almost certainly not in the cases you’re thinking of.

> [@CRquantum](#):
>
> Or, like, is it just using Int and Float is enough, and no need to particular do Int64 and Float64?

On 32 bit platforms, `Int` refers to `Int32`. On 64 bit platforms, `Int` refers to `Int64`. There is no equivalent shorthand for floating point numbers.

* * *

For variables, type assertions limit the type of that binding in the current scope to the asserted type.

For function arguments in function definitions, type assertions restrict the allowed incoming types for that argument.

For struct fields, type assertions signify that the field in question holds a value of the asserted type. The default constructors will try to convert incoming arguments to the types in question.

For more information, see the [section on type declarations in the docs](https://docs.julialang.org/en/v1/manual/types/#Type-Declarations).

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [August 9, 2021, 1:16am UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/6 "2021-08-09T01:16:36Z")

</div>

Have a look at Stefan’s answer [here](https://stackoverflow.com/questions/44435745/julia-abstract-and-concrete-types-integer-versus-int8-versus-int64)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 9, 2021, 5:14am UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/7 "2021-08-09T05:14:25Z")

</div>

> [@amrods](#):
>
> `function f1(n::Integer) = n + 1`

`1` is an `Int64`, so may want

```julia
function f1(n::Integer) = n + one(n) 

```

instead.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [August 9, 2021, 11:07am UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/8 "2021-08-09T11:07:17Z")

</div>

The answer strongly depends on the context the type annotation is used in.

#### Function signatures

Annotating argument type makes no performance difference, but adds dispatch methods. The general recommendation is to use concrete types in function signatures only for dispatch, and use a possibly loose type bounds otherwise to keep the functions generic. In translating code from Fortran, I’d recommend using `Integer` and `Real` type annotations for the beginning to easier keep track what is what.

#### Variable declaration within functions

Usually type declaration is redundant but you need to watch for type stability for performance. Sometimes, a nudge to the type inference algorithm may be useful, though. For performance, the types must be concrete or small unions.

#### Structure type members

Concrete types are absolutely crucial for performance.

### Type parameters / generics

The advises on how to annotate function argument types and local variable types are contradictory. So, you may go without any type annotations for local variables (what’s usually done anyways) or, if you feel uneasy about it, you could use types as parameters in signatures and function body (if you continue using Julia, you’ll need to learn it in any case, so this is just one of many examples)

```julia-auto
# Bad: input type is too tight
function cube(x::Float64)
    local square::Float64 = x * x
    return square * x
end

# Better but local variable type is too loose. 
# Does not help with type instabilities within the abstract type
function cube(x::Number)
    local square::Number = x * x
    return square * x
end

# Good: concrete type is parameterized.
# Function is now generic, and all local variable types are concrete.
# A drawback, of course, is that {T<:Number} is also too tight
# (it could include matrices, rotations, etc)
function cube(x::T) where {T<:Number}
    local square::T = x * x
    return square * x
end

```

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [August 9, 2021, 2:54pm UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/9 "2021-08-09T14:54:51Z")

</div>

```julia
function cube(x::Number) 
    local square::Number = x * x
    return square * x
end

```

What makes it too loose?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 9, 2021, 2:56pm UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/10 "2021-08-09T14:56:46Z")

</div>

> [@Vasily\_Pisarev](#):
>
> ```julia
> # Good: concrete type is parameterized.
> # Function is now generic, and all local variable types are concrete.
> # A drawback, of course, is that {T<:Number} is also too tight
> # (it could include matrices, rotations, etc)
> function cube(x::T) where {T<:Number}
> local square::T = x * x
> return square * x
> end
> 
> ```

Note: 99% of the time, you don’t need to add a type parameter to `local square::T`. Julia will infer this just fine.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [August 9, 2021, 4:52pm UTC](https://discourse.julialang.org/t/do-i-need-to-specify-concrete-type-of-a-variable-such-as-int64-or-just-int-float64-or-just-float/66047/11 "2021-08-09T16:52:21Z")

</div>

Turns out, abstract type annotations don’t stop the compiler from narrowing down the type, so it must be the same as untyped version performance-wise.  
But it won’t catch type instabilities or inefficiencies within the abstract type annotation, if that was the intent.  
I’ll edit my post accordingly, thanks for pointing that out.
