Skip to content

Instantly share code, notes, and snippets.

@joshday
Created October 19, 2023 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshday/4a3d852787743961fc9aaa9407d6f86b to your computer and use it in GitHub Desktop.
Save joshday/4a3d852787743961fc9aaa9407d6f86b to your computer and use it in GitHub Desktop.
Weird behavior with getproperty
julia> struct Wrapper
dict::Dict{Symbol, Any}
end
julia> Base.getproperty(w::Wrapper, k::Symbol) = get!(getfield(w, :dict), k, Wrapper(Dict{Symbol,Any}()))
julia> Base.setproperty!(w::Wrapper, k::Symbol, val) = setindex!(getfield(w, :dict), val, k)
julia> w = Wrapper(Dict())
Wrapper(Dict{Symbol, Any}())
julia> w.f = () -> 1
#3 (generic function with 1 method)
julia> w.f() = 5
WARNING: Method definition #3() in module Main at REPL[5]:1 overwritten at REPL[6]:1.
julia> w.f2() = 6
julia> w.f()
5
julia> w.f2()
6
julia> w() # how did this happen?
6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment