# 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:** 1
**Showing post:** 5

<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.

---

_[View the full topic](https://discourse.julialang.org/t/private-properties-in-julia-0-7/10785)._
