Skip to content

Instantly share code, notes, and snippets.

@astrieanna
Created December 7, 2013 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astrieanna/7845279 to your computer and use it in GitHub Desktop.
Save astrieanna/7845279 to your computer and use it in GitHub Desktop.
Trying to make `methodswith` work for a user-defined type.
module Test
export TwoVals, addtwovals
type TwoVals
val1
val2
end
function addtwovals(v::TwoVals)
v.val1 + v.val2
end
function addtwovals(v1::TwoVals, v2::TwoVals)
v1.val1 + v1.val2 + v2.val1 + v2.val2
end
v1 = TwoVals(1,2)
v2 = TwoVals(100,200)
# Use functions
println(addtwovals(v1))
println(addtwovals(v1, v2))
# Discover functions
println(methods(addtwovals))
println("methodswith inside module:")
methodswith(TwoVals)
methodswith(STDOUT,TwoVals,current_module(),false)
end
println("using Test:")
using Test
methodswith(Test.TwoVals)
methodswith(TwoVals,Test)
methodswith(STDOUT,TwoVals,Test,false)
println("Inlining methodswith implementation:")
m = Test
t = TwoVals
io = STDOUT
showparents = false
for nm in names(m)
try
mt = eval(nm)
d = mt.env.defs
while !is(d,())
if any(map(x -> x == t || (showparents && t <: x && x != Any && x != ANY && !isa(x, TypeVar)), d.sig))
print(io, nm)
show(io, d)
println(io)
end
d = d.next
end
println()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment