As a Fortran and rencently Julia programmer, which admire both, I will give one example of one the fundamental qualities of Julia. For example, let us define a function that computes a “energy” between two sets of points (the sum of 1/d for every pair):
julia> function energy(v1,v2)
u = 0
for x in v1
for y in v2
u += 1 / norm(x-y)
end
end
return u
end
energy (generic function with 1 method)
This function is completely agnostic for the type of variable which is input. For example, if we have two one-dimensional sets of points, we would do:
julia> norm(x::Float64) = abs(x)
norm (generic function with 2 methods)
julia> v1 = rand(100); v2 = rand(100);
julia> energy(v1,v2)
309242.371744854
What we did is defining what norm means of the type of input we are providing. Now let us do the same for three-dimensional particles we invented now:
julia> struct Particle
x::Float64
y::Float64
z::Float64
end
julia> norm(p::Particle) = sqrt(p.x^2 + p.y^2 + p.z^2)
norm (generic function with 2 methods)
julia> import Base.-
julia> -(p1::Particle,p2::Particle) = Particle(p1.x-p2.x,p1.y-p2.y,p1.z-p2.z)
- (generic function with 198 methods)
julia> v1 = [ Particle(rand(3)...) for i in 1:100 ];
julia> v2 = [ Particle(rand(3)...) for j in 1:100 ];
julia> energy(v1,v2)
19187.000577283474
What we did there is to define the Particle type, define what does it mean to compute the norm and the subtraction of such particles, and that is enough to make the energy function, without any modification, to work. The above code is also performant. (as a side note, the indexing of the arrays is completely irrelevant there).
That possibility of writing generic code is a very powerful concept. When one writes code for a specific application, where all types of things are known, that might be not very interesting. But the Julia ecosystem thrives of implementations of algorithms which originally thought to a specific problem, can be applied with no modification whatsoever to a number of other applications.
As someone who came to admire Julia quite a lot, it is true that I fear the fact that its development is still dependent on a handful of very talented people, but still only a handful of people. I would not advice anyone to not learn Fortran if aiming to do high-performance numerical computing. But I would advice to also learn Julia. The mental model, issues and solutions that appear in each language help a lot to be a better coder in the other. And their syntax are similar (both very natural IMO).
…
The strictly objective advantages of Fortran over Julia that I can see now are:
- Generate small and standalone binaries.
- Once compiled, runs fast (no compilation overhead), which may be important for a program that has to be executed thousands of times.
- Better debug and error messages (type declarations surely help in this).
- It is likely that a beginner will obtain faster code in the first attempts to write something in Fortran than in Julia. In Julia one needs to learn some of a mental model about how to obtain type-stable code and avoid allocations (for example, in the above, removing the
Float64 from Particle elements results in a code which is 60x slower). That has a learning curve, it is common that novices are frustrated with performance on their first attempts. But learning those concepts will help you to write better Fortran (and better Python, Java, etc as well).