Is there a way to make a mutable-struct read-only in a given scope? (I don’t need that to be strictly impossible, just not user-friendly).
Something like having:
struct A
x::Vector{Int}
end
and making it hard to use setindex
on x
of an instance of A
. The use case is having a callback function in an algorithm, and in this callback function the user would have access to variables which he/she should be able to read, but not to modify.
(I guess this is mostly the case of all callback functions in optimization algorithms - and we just trust the users to not modify the data).
I imagined the possibility of creating a custom array type to wrap x
, and thus expose
struct A
x::ReadOnlyArray{Vector{Int}}
end
without defining setindex
for ReadOnlyArray
. (now that I wrote the name it feels that someone invented this already…). But implementing an array interface seems a bit too much.