Let us assume that we have an abstract type
abstract type Person end
and we want that the following queries are available to all Person types:
My current approach (inspired by https://github.com/JuliaLang/julia/issues/6975) to force the users to implement these two queries is to define a macro
macro abstractmethod() quote error("This function belongs to an interface definition and cannot be used.") end end
and then define
sayhello(::Person)::String = @abstractmethod saybyebye(::Person)::String = @abstractmethod
Then, if we define, e.g.,
struct AncientRomanPerson <: Person end sayhello(::AncientRomanPerson) = "Salvete omnes!"
but we forget to implement
saybyebye(::AncientRomanPerson), we will get an error:
julius = AnceientRomanPerson() sayhello(julius) # -> Salvete omnes! saybyebye(julius) # -> error!!
My question is: is there any way to get an error before the
saybyebye method is called? Namely, when the instance
julius is created??
In python this is possible using the
abc package that provides the
ABC class and the
If someone thinks that it is a good idea (and it is possible) to implement something similar in julia to the
abc python package, I would help with pleasure.
Thanks for the help!