# Forcing users to implements all methods in an abstract interface

**URL:** <https://discourse.julialang.org/t/forcing-users-to-implements-all-methods-in-an-abstract-interface/23687>\
**Category:** General Usage\
**Created:** [April 30, 2019, 7:19am UTC](https://discourse.julialang.org/t/forcing-users-to-implements-all-methods-in-an-abstract-interface/23687 "2019-04-30T07:19:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fverdugo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fverdugo/32/9446_2.png) [@fverdugo](https://discourse.julialang.org/u/fverdugo)\
**Post date:** [April 30, 2019, 7:19am UTC](https://discourse.julialang.org/t/forcing-users-to-implements-all-methods-in-an-abstract-interface/23687/1 "2019-04-30T07:19:20Z")

</div>

Let us assume that we have an abstract type

```julia
abstract type Person end

```

and we want that the following queries are available to all Person types:

```julia
sayhello(::Person)::String
saybyebye(::Person)::String

```

My current approach (inspired by [Interfaces for Abstract Types · Issue #6975 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/6975)) to force the users to implement these two queries is to define a macro

```julia
macro abstractmethod()
  quote
    error("This function belongs to an interface definition and cannot be used.")
  end
end

```

and then define

```julia
sayhello(::Person)::String = @abstractmethod
saybyebye(::Person)::String = @abstractmethod

```

Then, if we define, e.g.,

```julia
struct AncientRomanPerson <: Person end
sayhello(::AncientRomanPerson) = "Salvete omnes!"

```

but we forget to implement `saybyebye(::AncientRomanPerson)`, we will get an error:

```julia
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!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 30, 2019, 7:31am UTC](https://discourse.julialang.org/t/forcing-users-to-implements-all-methods-in-an-abstract-interface/23687/2 "2019-04-30T07:31:02Z")

</div>

Effectively, you are asking Julia to do static checks.

This is not how the language was designed.

I think that even with the macro, you are overthinking it. If a type that should implement an interface doesn’t, then that’s a bug in the code that defines the type and should just be fixed. `MethodError` is fine for that purpose.

Perhaps in the package that introduces the API, you can just provide a function that helps unit testing an interface.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 30, 2019, 7:45am UTC](https://discourse.julialang.org/t/forcing-users-to-implements-all-methods-in-an-abstract-interface/23687/3 "2019-04-30T07:45:55Z")

</div>

A pattern that works well for me is to use unit tests: implement a `testisperson` function that checks all the interface is implemented.

PS your `abstractmethod` macro could just be a function.
