I have a question why methods with doesn’t return functions from other modules?
module EE
export identify
identify(x::T) where T = x
end
module DD
export f
export PP
using InteractiveUtils
using ..EE
struct PP{T}
x::T
end
identify(x::T) where T<:PP = println(x.x)
end
julia> using ..DD
julia> methodswith(PP)
I was expecting it to return identify function. however it doesn’t. Why is that?
Since you don’t import
the function identify
in DD
, it is not the same function.
1 Like
There are several issues here:
-
as said by Tamas, DD.identify
is not the same function as EE.identify
. You probably want to fix this, but it has not much to do with the fact that methodswith(PP)
does not list identify
.
-
again mostly unrelated, but one dot would be enough in using .DD
-
now more to the point: identify
is not exported by DD
, so you don’t have any identity
function readily available after using .DD
-
finally, quoting the methodswith
documentation: “The optional second argument restricts the search to a particular module or function (the default is all top-level modules).” Since DD is not a top-level module, methodswith
wont list its functions by default.
With all this in mind, the following examples should work:
1- only one module
module DD
export PP, identify
struct PP{T}
x::T
end
identify(x::T) where T<:PP = println(x.x)
end
using .DD
methodswith(PP, DD)
2- two modules
module EE
export identify
identify(x) = x
end
module DD
export PP
# extend EE.identify, which is exported by EE
import ..EE: identify
struct PP{T}
x::T
end
identify(x::T) where T<:PP = println(x.x)
end
using .EE # identify
using .DD # PP
methodswith(PP, EE) # identify is now owned by EE, which is still not
# a top level module
3 Likes