# Private Properties in Julia 0.7

**URL:** <https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785>\
**Category:** General Usage\
**Created:** [May 8, 2018, 5:25pm UTC](https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785 "2018-05-08T17:25:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [May 8, 2018, 5:25pm UTC](https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785/1 "2018-05-08T17:25:09Z")

</div>

Julia 0.7 supports method propertynames(x, private=false)  
The latest documentation for that method reads

‘‘propertynames(x) may return only “public” property names that are part of the documented interface of x. If you want it to also return “private” fieldnames intended for internal use, pass true for the optional second argument. REPL tab completion on x. shows only the private=false properties.’’

Does Julia 0.7 let you define a property/field as readonly when you define a `(mutable) struct`,  
or is the overloaded method `propertyname` responsible for enforcing `private=true`?

How does the REPL know when a property is private?

Also, how are methods `getproperty`, and `setproperty` expected to deal with private properties?

Thanks.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 8, 2018, 5:45pm UTC](https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785/2 "2018-05-08T17:45:24Z")

</div>

This has no influence on running code, it is purely for documentation and things like tab completion.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [May 8, 2018, 5:48pm UTC](https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785/3 "2018-05-08T17:48:35Z")

</div>

OK, I take it that the property related methods will have to enforce `private=true` if desired.  
But how does “tab completion” (i.e. the REPL) know that a property is supposed to be private?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 8, 2018, 6:05pm UTC](https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785/4 "2018-05-08T18:05:18Z")

</div>

It only looks at the `propertynames(x, private=false)`. It’s up to `x` to return the appropriate thing. Right now, the default definition for a struct will return all its fields.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [January 6, 2019, 11:27am UTC](https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785/5 "2019-01-06T11:27:52Z")

</div>

Can create private properties by defining:

```julia
struct MyType
    a
    b
end

function getpublicproperties(x::MyType, field::Symbol)
    if field == :a
        getfield(x, :a)
    else
        error("Not a public property")
    end
end

function getallproperties(x::MyType, field::Symbol)
    getfield(x, field)
end

function Base.getproperty(x::MyType, field::Symbol; public = true)
    if public
        getpublicproperties(x, field)
    else
        getallproperties(x, field)
    end
end

```

then you get:

```julia
julia> x = MyType(1, 2)
MyType(1, 2)

julia> x.a
1

julia> x.b
ERROR: Not a public property
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] getpublicproperties at ./REPL[2]:5 [inlined]
 [3] #getproperty#4 at ./REPL[8]:3 [inlined]
 [4] getproperty(::MyType, ::Symbol) at ./REPL[8]:2

julia> getproperty(x, :b, public = false)
2

```

I am sure you can do some macro-magic to define these things nicely.
