Say I have an abstract type with some structs, and I want to make sure some method is defined on every subtype. For example:
abstract type Shape end
struct Square <: Shape
length::Float64
end
struct Circle <: Shape
radius::Float64
end
area(s::Square) = s.length * s.length
area(c::Circle) = c.radius * c.radius * pi
area_plus(s::Square, y::Float64) = area(s) + y
area_plus(c::Circle, x::Float64, y::Float64) = area(c) + x + y
area_times(s::Square, y::Float) = area(s) * y
Then I’d like to be able to write a function method_exists
so that
all_shapes = [Square, Circle]
method_exists(area, all_shapes) # == true
method_exists(area_plus, all_shapes) # == true
method_exists(area_times, all_shapes) # == false
How can I write something that checks for this? hasmethod
gets me part of the way there, but it requires the whole signature of the function so I couldn’t find a way to apply it to something like area_plus
.
(In real life, we have several dozen “Shapes” and different researchers writing new ones, so I want a quick way to test that each Shape is at least well-formed.)