Note that while StaticArray
s are indeed static and immutible, they are designed primarily for linear algebra efficiency on small arrays, not necessarily for “protecting” their contents. You might notice unnecessarily long compile times if you use them.
Julia, being true to its open nature, does not have any concept of “privacy”, even the immutability of struct
is more for memory management than for immutability for its own sake.
If you want, you can build a custom immutable array type really easily
struct LockArray{T,N,V<:AbstractArray{T,N}} <: AbstractArray{T,N}
data::V
end
Base.size(v::LockArray) = size(v.data)
Base.getindex(v::LockArray{T,N}, i::Vararg{Int,N}) where {T,N} = v.data[i...]
function Base.setindex!(v::LockArray{T,N}, x, i::Vararg{Int,N}) where {T,N}
throw(ArgumentError("Don't touch my array!!"))
end
julia> v = LockArray(rand(3,3,3));
julia> typeof(v)
LockArray{Float64,3,Array{Float64,3}}
julia> v[1,1,1] = Inf
ERROR: ArgumentError: Don't touch my array!!
Stacktrace:
[1] setindex!(::LockArray{Float64,3,Array{Float64,3}}, ::Float64, ::Int64, ::Int64, ::Int64) at /home/msavas200/src/scrap.jl:9
[2] top-level scope at none:0
More likely however, you’re much better off just not worrying about it at all.