This is very close to what I need. Thanks! On Thu, May 19, 2016 at 10:00 AM, Kristoffer Carlsson <kcarlsso...@gmail.com > wrote:
> Sounds like: > https://github.com/JuliaLang/DataStructures.jl/blob/master/src/delegate.jl > > In action: > https://github.com/JuliaLang/DataStructures.jl/blob/f67dc0b144d1fcc83eb48a7beddca746d4841f9e/src/default_dict.jl#L52 > > On Thursday, May 19, 2016 at 3:53:35 PM UTC+2, Tom Breloff wrote: >> >> I have a pattern that I frequently use, and I'm considering writing a >> macro to automate it, but figured I'd check if it exists already. >> >> I frequently want to have the equivalent of an existing type >> functionally, but I want to be able to either dispatch on it or include >> additional fields for other purposes. An example is wrapping a dictionary: >> >> julia> immutable MyDict{K,V} <: Associative{K,V} >> d::Dict{K,V} >> otherfield::Int >> end >> >> >> julia> md = MyDict(Dict(:x=>1), 10) >> Error showing value of type MyDict{Symbol,Int64}: >> ERROR: MethodError: `length` has no method matching length(::MyDict{ >> Symbol,Int64}) >> >> >> julia> md[:x] >> ERROR: MethodError: `get` has no method matching get(::MyDict{Symbol, >> Int64}, ::Symbol, ::Symbol) >> Closest candidates are: >> get(::ObjectIdDict, ::ANY, ::ANY) >> get{K,V}(::Dict{K,V}, ::Any, ::Any) >> get{K}(::WeakKeyDict{K,V}, ::Any, ::Any) >> ... >> in getindex at dict.jl:282 >> in eval at REPL.jl:3 >> >> >> julia> Base.length(md::MyDict) = length(md.d) >> length (generic function with 111 methods) >> >> >> julia> md >> MyDict{Symbol,Int64} with 1 entryError showing value of type MyDict{ >> Symbol,Int64}: >> ERROR: MethodError: `start` has no method matching start(::MyDict{Symbol, >> Int64}) >> in isempty at iterator.jl:3 >> in showdict at dict.jl:93 >> in writemime at replutil.jl:36 >> in display at REPL.jl:114 >> in display at REPL.jl:117 >> [inlined code] from multimedia.jl:151 >> in display at multimedia.jl:163 >> in print_response at REPL.jl:134 >> in print_response at REPL.jl:121 >> in anonymous at REPL.jl:624 >> in run_interface at ./LineEdit.jl:1610 >> in run_frontend at ./REPL.jl:863 >> in run_repl at ./REPL.jl:167 >> in _start at ./client.jl:420 >> >> # the pain continues... >> >> >> >> I'd like to be able to automatically do "md[:x]" and have it work, but >> instead I have to define a bunch of Base methods where I simply re-call the >> method with "md.d". >> >> The macro I have in mind would grab the immediate supertype of the type >> in question, make the new type a subtype of that supertype, and then define >> pass-through methods for anything in the "methodswith(Dict)" list. >> >> Does this exist already? Could I get myself in trouble somehow by >> defining pass-through methods like this? >> >> Thanks! >> Tom >> >