CRquantum
CRquantum

Reputation: 626

Can I define variable in Julia just like in Fortran

I am new to Julia. Have a quick question. In Fortran, we can do

implicit none

Therefore whenever we use a variable, we need to define it first. Otherwise it will give error.

In Julia, I want to do the same time thing. I want to define the type of each variable first, like Float64, Int64, etc. So that I wish that Julia no longer need to automatically do type conversion, which may slow down the code. Because I know if Julia code looks like Fortran it usually runs fast.

So, in short, is there a way in Julia such that, I can force it to only use variables whose types has been defined, and otherwise give me an error message? I just want to be strict.

I just wanted to define all the variables first, then use them. Just like Fortran does.

Thanks!

[Edit]

As suggested by the experts who answers the questions, thank you very much! I know that perhaps in Julia there is no need to manually define each variables one by one. Because for one thing, if I do that, the code will become just like Fortran, and it can be very very long especially if I have many variables.

But if I do not define the type of the each variables, is Julia smart enough to know the type? Will it do some conversions which may slow down the code?

Also, even if there is no need to define variables one by one, is there in some situations we may have to manually define the type manually?

Upvotes: 2

Views: 553

Answers (2)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

Strict type declaration is not how you achieve performance in Julia. You achieve the performance by type stability. Moreover declaring the types is usually not recommended because it decreases interoperability.

Consider the following function:

f(x::Int) = rand() < 4+x ? 1 : "0"

The types of everything seem to be known. However, this is a terrible (from performance point of view) function because the type of output can not be calcuated by looking types of input. And this is exactly how you write the performant code with regard to types.

So how to check your code? There is a special macro @code_warntype to detect such cases so you can correct your code:

enter image description here

Another type related issue are the containers that should not be specified by abstract elements. So you never want to have neither Vector{Any} nor Vector{Real} - rather than that you want Vector{Int} or Vector{Float64}.

See also https://docs.julialang.org/en/v1/manual/performance-tips/ for further discussion.

Upvotes: 1

cbk
cbk

Reputation: 4370

No, this is not possible* as such. You can, however, add type annotations at any point in your code, which will raise an error if the variable is not of the expected type:

julia> i = 1
1

julia> i::Int64
1

julia> i = 1.0
1.0

julia> i::Int64
ERROR: TypeError: in typeassert, expected Int64, got a value of type Float64
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

julia> i=0x01::UInt8
0x01

*Julia is a dynamically-typed language, though there are packages such as https://github.com/aviatesk/JET.jl for static type-checking (i.e., no type annotations required) and https://github.com/JuliaDebug/Cthulhu.jl for exploring Julia's type inference system.

Upvotes: 1

Related Questions