Created
October 19, 2023 13:24
-
-
Save joshday/4a3d852787743961fc9aaa9407d6f86b to your computer and use it in GitHub Desktop.
Weird behavior with getproperty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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