Not Seeing Expected Results
Hi. I'm the person on julia-users who was asking for something like methodswithdescendants. Not sure where to post this - probably a mistake of mine more than an issue with your code. But when I try to use your package, as shown at https://github.com/andrewcooke/IntModN.jl/blob/e2f4f39729ceec0fe485da372acb827982870a23/src/IntModN.jl#L192 I see the following output (only):
Integer(:ZR,0.125)
(:ZF,0.125)
ZRing
This seems to be something to do with context. If I run
julia> using TypeCheck
julia> methodswithdescendants(Integer, lim=20)
20-element Array{(Symbol,Float64),1}:
(:<,1.0)
(:<=,1.0)
...
at the top level prompt then everything works as expected. The problem above occurs when I run the code as julia IntModN.jl
Is this a known limitation? Is there any way to work around it? Ideally I would like to programaticaly check (in a test) that these lists are similar in some way...
Thanks, Andrew
Also, if I try to test my code at the top level, I see hardly any functions:
julia> using TypeCheck, IntModN
test_constructor ok
test_arithmetic ok
test_matrix_inverse ok
test_power ok
Integer(:ZR,0.125)
(:ZF,0.125)
ZRing
test_coverage ok
julia> methodswithdescendants(ZRing, lim=20)
1-element Array{(Symbol,Float64),1}:
(:convert,1.0)
julia> methodswithdescendants(Z, lim=20)
1-element Array{(Symbol,Float64),1}:
(:convert,0.6666666666666666)
julia> GF2(1) + GF2(1)
0 mod 2
Despite addition working (last line) the + function is not listed for ZRing (or ZField, which GF2 is).
Confused, Andrew
methodswithdescendants depends on methodswith showing the functions.
I see the same behavior as you:
julia> using IntModN
test_constructor ok
test_arithmetic ok
test_matrix_inverse ok
test_power ok
Integer(:ZR,0.125)
(:ZF,0.125)
ZRing
test_coverage ok
julia> using TypeCheck
The reason that methodswithdescendants is giving you that output at the top level, it that this is what methodswith is returning:
julia> methodswith(ZRing)
1-element Array{Method,1}:
convert{X<:Integer}(::Type{X<:Integer},z::ZRing{N,I<:Integer}) at /home/leah/src/test/IntModN.jl:67
julia> methodswith(ZField)
1-element Array{Method,1}:
convert{X<:Integer}(::Type{X<:Integer},z::ZField{N,I<:Integer}) at /home/leah/src/test/IntModN.jl:68
My guess is that methodswith doesn't seem to see the parameterized functions properly (the +{N,I} part). They're clearly in scope, since methods(+) at the REPL does return them. This seems like a bug in methodswith to me (or at least a nuance of it's behavior due to something I don't understand about the type/dispatch system).
The results of your println("Integer", methodswithdescendants(Integer, lim=20)) inside your module is explained by:
julia> methodswith(Int,IntModN)
4-element Array{Method,1}:
ZR(n::Int64,i::Integer) at /home/leah/src/test/IntModN.jl:50
ZR{I<:Integer}(n::Int64,i,::Type{I<:Integer}) at /home/leah/src/test/IntModN.jl:49
ZF(n::Int64,i::Integer) at /home/leah/src/test/IntModN.jl:52
ZF{I<:Integer}(n::Int64,i,::Type{I<:Integer}) at /home/leah/src/test/IntModN.jl:51
methodswith(t::DataType) looks for methods inside the current module and it's submodules; at the REPL, this means everything that's been imported, including Base. In your IntModN module, it will only look in IntModN. There is another variation, methodswith(t::DataType, m::Module) that looks for functions in the specified module; this could allow you to specify Base to see more functions from Integer's descendants.
To make methodswithdescendants(Integer, lim=20) work better, I can change it to deal with Modules differently. It could always look in Base or allow specifying which modules to look in or something.
thanks for the explanation. could it take a list of modules? should i raise an issue w julia about methodswith not finding + etc?
Yes, I can make it take a list of modules. It feels like there should be a cleaner way to make this scoping work, but a list should at least work.
Yes, the methodswith thing seems like a bug to me. Could you @-mention me on the issue, please?
don't do it if you think there could be a better way! i'll file an issue now.
done. also, weird moment, i follow the link to check, see your name, just as stefan karpinski mentions you in the video at http://vimeo.com/84661077 24m in.