# Mutable struct vs closure

**URL:** <https://discourse.julialang.org/t/mutable-struct-vs-closure/20233>\
**Category:** New to Julia\
**Tags:** struct, closure\
**Created:** [January 29, 2019, 2:17pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233 "2019-01-29T14:17:03Z")\
**Posts on this page:** 1\
**Showing post:** 21

<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:** [January 30, 2019, 11:29am UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/21 "2019-01-30T11:29:58Z")

</div>

Possibly, but Julia does not offer what you call traditional encapsulation, a mechanism designed to explicitly hide state (or make it difficult to access).

Instead, I try to stick to the following implicit rules:

1. only methods of the _same module_ should _modify_ fields of a structure,

2. reading fields is kind of borderline, if it is needed to done frequently, it should be done with an accessor, or documented as the recommended API.

If you are really paranoid, you could redefine

```julia
Base.getproperty(::MyObject, _) = error("no access to fields")

```

or similar. But you always have `Base.getfield` to circumvent this. There is really no way to protect people if they want to do something stupid.

---

_[View the full topic](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233)._
