# Help with \`Base.getproperty\` syntax

**URL:** <https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628>\
**Category:** General Usage\
**Created:** [October 22, 2018, 1:47pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628 "2018-10-22T13:47:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [October 22, 2018, 1:47pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/1 "2018-10-22T13:47:37Z")

</div>

I am trying to add `Base.property` methods to my own types for the first time and hitting a wall. Here’s a minimal example:

```julia
module A
    import Base.getproperty

    struct S
        x
    end

    Base.getproperty(x::S, ::Val{:a}) = "a"
end

obj = A.S(1)
obj.a

```

This gives the error:

```julia
ERROR: type S has no field a
Stacktrace:
 [1] getproperty(::Any, ::Symbol) at ./sysimg.jl:18

```

I am on Julia 1.0.1 on OSX. Thanks for any suggestions.

---

<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:** [October 22, 2018, 1:52pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/2 "2018-10-22T13:52:57Z")

</div>

```julia
julia> struct S
           x
       end

julia> function Base.getproperty(x::S, sym::Symbol)
           if sym == :a
               "a"
           else
               # fallback to getfield
               getfield(x, sym)
           end
       end

julia> obj = S(1)
S(1)

julia> obj.a
"a"

julia> obj.x
1

julia> obj.y
ERROR: type S has no field y
Stacktrace:
 [1] getproperty(::S, ::Symbol) at ./REPL[2]:6

```

---

<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:** [October 22, 2018, 1:57pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/3 "2018-10-22T13:57:53Z")

</div>

Note that this seems to be broken on master… [Overloading getproperty in REPL doesn't work (master) · Issue #29761 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/29761)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 22, 2018, 1:58pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/4 "2018-10-22T13:58:26Z")

</div>

(Note that due to ongoing GitHub issues, you may see that link as a 404—I do.)

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 22, 2018, 2:03pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/5 "2018-10-22T14:03:28Z")

</div>

You can still dispatch on the property name with

```julia
_getproperty(x::S, ::Val{s}) where {s} = getfield(x, s)
_getproperty(x::S, ::Val{:a}) = "a"

Base.getproperty(x::S, s::Symbol) = _getproperty(x, Val{s}())

```

However, you should only do this if you have a small number of property names, as there’s lots of potential for performance issues. It’s also a good idea to provide methods for `Base.propertynames`.

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [October 22, 2018, 2:12pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/6 "2018-10-22T14:12:36Z")

</div>

Thank you for the solution. Is this the intended way to use `Base.getproperty` or a workaround for a current issue? I would have had a hard time figuring this out from the docs. If it’s intended, I’ll submit a PR for the Julia docs.

---

<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:** [October 22, 2018, 2:59pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/7 "2018-10-22T14:59:53Z")

</div>

It is the intended way, cf

```julia
help?> getproperty
search: getproperty

  getproperty(value, name::Symbol)

  The syntax a.b calls getproperty(a, :b).

  See also propertynames and setproperty!.

```

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [October 23, 2018, 12:36pm UTC](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/8 "2018-10-23T12:36:10Z")

</div>

Thank you, added your tip here [https://github.com/JuliaLang/julia/pull/29771](https://github.com/JuliaLang/julia/pull/29771)
