# Semantics for shared structure

**URL:** <https://discourse.julialang.org/t/semantics-for-shared-structure/2271>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 24, 2017, 2:48pm UTC](https://discourse.julialang.org/t/semantics-for-shared-structure/2271 "2017-02-24T14:48:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 24, 2017, 2:48pm UTC](https://discourse.julialang.org/t/semantics-for-shared-structure/2271/1 "2017-02-24T14:48:00Z")

</div>

I have no formal background in CS, so I am not even sure I am framing this question right, but this comes up a lot in designing APIs for me.

Consider a simple type

```julia
immutable UsesNodes
    nodes
    somevalue
    function UsesNodes(nodes)
        @assert issorted(nodes)
        new(nodes, calculate_value(nodes))
    end
end

```

which checks the argument, and relies on it being not modified. `nodes` is created by the user, so in theory it could share structure and then be accidentally modified, as in

```julia
x = sort(rand(10))
u = UsesNodes(x)
# in a location far, far away
x[1] = 19.2 # seemed like a good idea

```

I can imagine two semantics:

1. _“paranoid”_: always make a copy, maybe provide a constructor option that doesn’t,
2. _“trusting”_: if you modify the value you have provided and your calculation breaks, it’s your problem.

I am trying to decide which one is more idiomatic for Julia, and have reviewed some code; but both approaches are present, eg [WeightVec in StatsBase](https://github.com/JuliaStats/StatsBase.jl/blob/master/src/weights.jl) is “trusting”, but some packages have extra options for non-copying constructors.

Is there a general approach that would be a good default? I am leaning towards “trusting”.
