Reflective programming/Metaprogramming challenge:
I am interested in exploring which methods are defined directly for some (usually parametric) and usually abstract types. To be clear, my interest is only in those methods defined with that particular type (with a UnionAll
or specific generic parameter) and not simply the list of methods with which the type could be called.
To simplify, we could only look for those that have the type in the first 2 (or whatever) arguments.
As an example, lets say I have the following types, which may be in some module
module MyModule
abstract type SomeSuper end
abstract type Pointy{T} <: SomeSuper end
struct Point{T} <: Pointy{T}
x::T
y::T
end
f1(x::SomeSuper) = 1
f2(x::SomeSuper) = 1
f2(x::Pointy{T}) where T = 2
f3(x::Pointy{Float64}) where T = 3
f4(x::Pointy{T}) where T = 1
end
f5(x::MyModule.Pointy{T}) where T = 1 # different namespace
f6(y, x::MyModule.Pointy{T}) where T = 1
f7(x::Point{T}) where T = 1 # note the concrete sub-type
We can call them as well, just to ensure all the names are loaded in whichever way necessayr
# calling works
p = MyModule.Point{Float64}(0.1, 0.2)
MyModule.f2(p)
MyModule.f3(p)
MyModule.f4(p)
f5(p)
f6(1, p)
f7(p)
We could even assume that they are available with using
if that helps
I want to be able to have something called getfunctions
which returns the list of functions that directly mention the type, or methods if that is easier. The signature would be something like
fs = getfunctions(MyModule.Pointy) # note abstract type without specific parameters
# should be
fs == [:f2, :f3, :f4, :f5, :f6] # could have namespace qualifications, if easier
In this, :f1
doesn’t match because there are no methods which mention Pointy directly, and :f7
doesn’t match because it only refers to sub-types of Pointy
. All of the other ones have an argument to a method which refers to either Pointy
with any parameter or a Pointy
with a particular generic parameter.
Any thoughts on whether this is possible or how it could be written? I think it would be a generally useful bit of code to explore the type-system.