Julia in-place modification and memory usages

I guess you are right that c .= a + b is probably almost always a typo and c .= a .+ b is what the programmer actually meant to write, but this phenomenon is specific to +. For other operations, whether or not you add the . on the right hand side can make a difference as to what result you obtain. Example:

julia> a = zeros(2,2)
       b = rand(2,2)

       display(a .= exp(b)) # Compute matrix exponential
       display(a .= exp.(b)) # Apply exp to each entry

2×2 Array{Float64,2}:
 1.46926   1.38004
 0.722834  1.96309
2×2 Array{Float64,2}:
 1.16331  2.53122
 1.6265   1.62189

The point then is that yes, Julia could try to be helpful and automatically replace a .= b + c with a .= b .+ c, but that would severely complicate the rules for when you have to add the dot and in the long run that would almost surely make things worse.

2 Likes