# \`getproperty\` with other types than \`Symbol\`

**URL:** https://discourse.julialang.org/t/getproperty-with-other-types-than-symbol/30729
**Category:** General Usage
**Tags:** question
**Created:** [November 5, 2019, 8:31am UTC](https://discourse.julialang.org/t/getproperty-with-other-types-than-symbol/30729 "2019-11-05T08:31:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![phg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phg/32/17184_2.png) [@phg](https://discourse.julialang.org/u/phg)
#### Post date: [November 5, 2019, 8:31am UTC](https://discourse.julialang.org/t/getproperty-with-other-types-than-symbol/30729/1 "2019-11-05T08:31:56Z")

</div>

I noticed that one can overload `getproperty` with `String`, not only `Symbol` (PyCall does this):

```julia
julia> struct Foo end

julia> import Base: getproperty

julia> getproperty(::Foo, x::Symbol) = x
getproperty (generic function with 22 methods)

julia> getproperty(::Foo, x::String) = x
getproperty (generic function with 23 methods)

julia> Foo().sdf
:sdf

julia> Foo()."sdf"
"sdf"

```

But it doesn’t work with, e.g., integers (this is even a parse error):

```julia
julia> getproperty(::Foo, x::Int) = x
getproperty (generic function with 24 methods)

julia> Foo().1
ERROR: syntax: extra token "0.1" after end of expression
Stacktrace:
 [1] top-level scope at REPL[11]:0

```

Is there any documentation about what types can (or should) be used?

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [November 5, 2019, 8:46am UTC](https://discourse.julialang.org/t/getproperty-with-other-types-than-symbol/30729/2 "2019-11-05T08:46:28Z")

</div>

The `getproperty` docstring seems to document usage only with symbols. In the integers case, you can quote them, e.g. `Foo().:1` .

---

<div class="post-metadata">

### Author: ![phg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phg/32/17184_2.png) [@phg](https://discourse.julialang.org/u/phg)
#### Post date: [November 5, 2019, 9:36am UTC](https://discourse.julialang.org/t/getproperty-with-other-types-than-symbol/30729/3 "2019-11-05T09:36:21Z")

</div>

Ah, that’s a good hint. Apparently, you can use any literals, as long as you quote them:

```julia
julia> Foo().:true
ERROR: MethodError: no method matching getproperty(::Foo, ::Bool)
Closest candidates are:
  getproperty(::Foo, ::Foo) at REPL[13]:1
  getproperty(::Foo, ::Int64) at REPL[11]:1
  getproperty(::Foo, ::String) at REPL[8]:1
  ...

```

Although there are some strange corner cases from the parser – the following two count as “just” symbols:

```julia
julia> Foo().:nothing
:nothing

julia> Foo().:notsdf
:notsdf
```

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [November 5, 2019, 9:51am UTC](https://discourse.julialang.org/t/getproperty-with-other-types-than-symbol/30729/4 "2019-11-05T09:51:26Z")

</div>

Note that `:true === true`, like for integers a quoted `Bool` is equal to itself. But `Foo().true` can work directly, as there is no parser ambiguity like with `Foo().1`. On the other hand, `:nothing` is a `Symbol`, i.e. `:nothing !== nothing`.
