# Defining array of constant values and prevent it to be modified inside functions

**URL:** <https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843>\
**Category:** New to Julia\
**Created:** [July 26, 2019, 12:30pm UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843 "2019-07-26T12:30:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [July 26, 2019, 12:30pm UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843/1 "2019-07-26T12:30:52Z")

</div>

Hi,

I am trying to understand the best approach to pass an array of user input data into a function and make sure that this array is NEVER modified inside the function.

An example of what I would like to do is below:

```julia
julia> struct UserStruct
           data::Array{Float64}
       end

julia> test=UserStruct([1.0 2.0 3.0]) ## The idea is to have these values fixed always
UserStruct([1.0 2.0 3.0])

julia> println(test.data)
[1.0 2.0 3.0]

julia> function TestFun( x::UserStruct)
           x.data[1] = 10.0 ## I don't this to be allowed here
       end
TestFun (generic function with 1 method)

julia> TestFun(test)
10.0

julia> println(test.data)
[10.0 2.0 3.0]

```

Could someone provide some comments / resources on how to achieve this goal ? Thanks in advance !

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [July 26, 2019, 12:32pm UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843/2 "2019-07-26T12:32:59Z")

</div>

Use StaticArrays? Or use fields of the type.

PS: probably use `data::Vector{Float64}`

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [July 26, 2019, 12:41pm UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843/3 "2019-07-26T12:41:09Z")

</div>

I think that’s the goal of this package: [GitHub - bkamins/ReadOnlyArrays.jl: A wrapper type around AbstractArray that is read-only](https://github.com/bkamins/ReadOnlyArrays.jl)

This doesn’t prevent modification absolutely, but the intention (I believe) is to prevent accidental modification. For example, given a ReadOnlyArrays view, a function could use `parent` to get the original array which it could then modify.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [July 26, 2019, 3:16pm UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843/4 "2019-07-26T15:16:08Z")

</div>

You can wrap an array with a struct and define `AbstractArray` interface with custom `setindex!` to disallow mutation but this will never guarantee immutability because someone can still access fields of the struct directly. Then, also, suppress `getproperty` but there is also `getfield`.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 26, 2019, 3:19pm UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843/5 "2019-07-26T15:19:22Z")

</div>

Perhaps a related question is: what exactly are you trying to protect against? Creating a custom `AbstractArray` with no `setindex!` will ensure that users cannot accidentally modify the internal data, and overloading `getproperty` can make it harder for them to accidentally peek at the underlying array, but it is always _possible_ for a sufficiently motivated user to break things.

---

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [July 26, 2019, 8:07pm UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843/6 "2019-07-26T20:07:07Z")

</div>

Ok thanks everyone for the reply. Going through your answers it appears that there is no straightforward way to do what I am looking for (at least is not clear to me).

@rdeits, what I am trying to protect is to introduce unintentional bugs in my code. The user data will be passed into a quite large function that perform several operations using input data. I just thought that it would help to reduce the risk that something gets modified unintentionally.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 26, 2019, 8:19pm UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843/7 "2019-07-26T20:19:43Z")

</div>

Are you sure? ReadOnlyArrays.jl seems like it does exactly what you’re looking for:

```julia
julia> using ReadOnlyArrays

julia> struct UserStruct
         data::ReadOnlyArray{Float64, 1, Array{Float64, 1}}
       end

julia> test = UserStruct(ReadOnlyArray([1.0, 2.0, 3.0]))
UserStruct([1.0, 2.0, 3.0])

julia> test.data[1] = 2.0
ERROR: setindex! not defined for ReadOnlyArray{Float64,1,Array{Float64,1}}

```

---

<div class="post-metadata">

**Author:** ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)\
**Post date:** [July 27, 2019, 11:35am UTC](https://discourse.julialang.org/t/defining-array-of-constant-values-and-prevent-it-to-be-modified-inside-functions/26843/8 "2019-07-27T11:35:58Z")

</div>

@rdeits, thanks ! I see now that you are correct and that the solution can be obtained using the [ReadOnlyArrays](https://github.com/bkamins/ReadOnlyArrays.jl), as pointed out by @ericphanson

Thanks for showing it with an example, it made the point much easier to understand.
