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:
sayhello(::Person)::String
saybyebye(::Person)::String
My current approach (inspired by Interfaces for Abstract Types · Issue #6975 · JuliaLang/julia · GitHub) 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 @abstractmethod
decorator…
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!