How can I define a new type in a new package that extends an abstract type in an old package such that functions in the old package work when passed objects of the new type?
I have a type AbstractBody
in WaterLily and I have a routine measure!
that should work for any subtype as long as they implement a few functions (sdf
and measure
). I define AutoBody<:AbstractBody
within the same package, then define sdf
and measure
for that type, and everything works fine.
But I want to define other special-purpose types like a ParametricBody<:WaterLily.AbstractBody
in their own packages. I’ve defined the sdf
and measure
functions, but WaterLily still can’t use them. For example:
using WaterLily,ParametricBodies,StaticArrays
R = 6
surf(θ,t) = R*SA[cos(θ+t),sin(θ+t)]
locate(x::SVector{2},t) = atan(x[2],x[1])-t
body = ParametricBody(surf,locate)
sdf(body,SA[1.,1.],0.) # no problem, returns -4.58
Simulation((8,8),(1,0),R;body) # throws error
The sdf
functions works fine in the REPL, but the Simulation
doesn’t know how to deal with a ParametricBody
!
MethodError: no method matching sdf(::ParametricBody{typeof(surf), typeof(locate), ParametricBodies.var"#12#13", Float64}, ::SVector{2, Float64}, ::Int64)
Closest candidates are: sdf(::AutoBody, ::Any, ::Any)
It must be possible to extend Abstract types like this… What am I doing wrong?