Re: [julia-users] metaprogramming and encapsulation
It is not in general possible to recover the function itself from a method, as methods are specific to types and not functions. Multiple functions can share methods if those functions have the same type. In the case of generic (or singleton) functions, you can recover the function from the instance field of the type of the method's first parameter. That is, first(method).sig.parameters[1].instance On Sunday, October 2, 2016 at 11:41:12 AM UTC-4, Carlo Lucibello wrote: > > that could be handy indeed, thanks. > I would also like to automatize the process using > ```julia > mlist = methodswith(A, ModA) > ``` > but looks like objects of type Method are not callable > ```julia > julia> mlist[1](1) > ERROR: MethodError: objects of type Method are not callable > in eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:64 > in macro expansion at ./REPL.jl:95 [inlined] > in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:68 > ``` > How do I go from a Method object to the corresponing callable Function > object? > > > Il giorno lun 26 set 2016 alle ore 16:39 Mauro> ha scritto: > >> This macro might work for you >> >> https://github.com/JuliaLang/DataStructures.jl/blob/8422ca9f66bfaa0f257b2bbbc8637c844d49ea68/src/delegate.jl >> >> On Mon, 2016-09-26 at 16:31, Carlo Lucibello > > wrote: >> > Hi everybody, >> > I'm looking for a metaprogramming trick to solve the following problem >> (I'm >> > confident it can be done efficiently with julia) . Let's say I have >> defined a >> > type in a module along with some methods: >> > module ModA >> > export A,f1,f2,f3 >> > >> > type A >> > ... >> > end >> > >> > f1(a::A, x::Int) = ... >> > f2(x, a::A, y) = ... >> > f3(a1::A,a2::A) = ... >> > >> > end #module >> > Now I have a second type that encapsulate the first >> > >> > using ModA >> > >> > type B >> > a::A >> > ... >> > end >> > >> > and I would like to define a macro that automatically defines all the >> exported >> > function in ModA for the type B in the trivial way, that is >> > >> > f1(b::B, x::Int) = f1(b.a, x) >> > f2(x, b::B, y) = f1(x, b.a, y) >> > f3(b1::B, b2::B) = f1(b1.a, b2.a) >> > >> > A starting point could be methodswith(A, ModA), but I don't know how to >> procede >> > further. Suggestions? >> > >> > Bye, >> > Carlo >> >
Re: [julia-users] metaprogramming and encapsulation
that could be handy indeed, thanks. I would also like to automatize the process using ```julia mlist = methodswith(A, ModA) ``` but looks like objects of type Method are not callable ```julia julia> mlist[1](1) ERROR: MethodError: objects of type Method are not callable in eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:64 in macro expansion at ./REPL.jl:95 [inlined] in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:68 ``` How do I go from a Method object to the corresponing callable Function object? Il giorno lun 26 set 2016 alle ore 16:39 Mauroha scritto: > This macro might work for you > > https://github.com/JuliaLang/DataStructures.jl/blob/8422ca9f66bfaa0f257b2bbbc8637c844d49ea68/src/delegate.jl > > On Mon, 2016-09-26 at 16:31, Carlo Lucibello > wrote: > > Hi everybody, > > I'm looking for a metaprogramming trick to solve the following problem > (I'm > > confident it can be done efficiently with julia) . Let's say I have > defined a > > type in a module along with some methods: > > module ModA > > export A,f1,f2,f3 > > > > type A > > ... > > end > > > > f1(a::A, x::Int) = ... > > f2(x, a::A, y) = ... > > f3(a1::A,a2::A) = ... > > > > end #module > > Now I have a second type that encapsulate the first > > > > using ModA > > > > type B > > a::A > > ... > > end > > > > and I would like to define a macro that automatically defines all the > exported > > function in ModA for the type B in the trivial way, that is > > > > f1(b::B, x::Int) = f1(b.a, x) > > f2(x, b::B, y) = f1(x, b.a, y) > > f3(b1::B, b2::B) = f1(b1.a, b2.a) > > > > A starting point could be methodswith(A, ModA), but I don't know how to > procede > > further. Suggestions? > > > > Bye, > > Carlo >
Re: [julia-users] metaprogramming and encapsulation
This macro might work for you https://github.com/JuliaLang/DataStructures.jl/blob/8422ca9f66bfaa0f257b2bbbc8637c844d49ea68/src/delegate.jl On Mon, 2016-09-26 at 16:31, Carlo Lucibellowrote: > Hi everybody, > I'm looking for a metaprogramming trick to solve the following problem (I'm > confident it can be done efficiently with julia) . Let's say I have defined a > type in a module along with some methods: > module ModA > export A,f1,f2,f3 > > type A > ... > end > > f1(a::A, x::Int) = ... > f2(x, a::A, y) = ... > f3(a1::A,a2::A) = ... > > end #module > Now I have a second type that encapsulate the first > > using ModA > > type B > a::A > ... > end > > and I would like to define a macro that automatically defines all the exported > function in ModA for the type B in the trivial way, that is > > f1(b::B, x::Int) = f1(b.a, x) > f2(x, b::B, y) = f1(x, b.a, y) > f3(b1::B, b2::B) = f1(b1.a, b2.a) > > A starting point could be methodswith(A, ModA), but I don't know how to > procede > further. Suggestions? > > Bye, > Carlo
[julia-users] metaprogramming and encapsulation
Hi everybody, I'm looking for a metaprogramming trick to solve the following problem (I'm confident it can be done efficiently with julia) . Let's say I have defined a type in a module along with some methods: module ModA export A,f1,f2,f3 type A ... end f1(a::A, x::Int) = ... f2(x, a::A, y) = ... f3(a1::A,a2::A) = ... end #module Now I have a second type that encapsulate the first using ModA type B a::A ... end and I would like to define a macro that automatically defines all the exported function in ModA for the type B in the trivial way, that is f1(b::B, x::Int) = f1(b.a, x) f2(x, b::B, y) = f1(x, b.a, y) f3(b1::B, b2::B) = f1(b1.a, b2.a) A starting point could be methodswith(A, ModA), but I don't know how to procede further. Suggestions? Bye, Carlo