# Design pattern for correctly overwriting abstract methods (interface methods)

**URL:** <https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778>\
**Category:** General Usage\
**Created:** [January 25, 2020, 12:02pm UTC](https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778 "2020-01-25T12:02:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [January 25, 2020, 12:02pm UTC](https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778/1 "2020-01-25T12:02:41Z")

</div>

I have something like the following situation:

```julia
module Cache

export cache

function cache(data)
  throw(NotImplementedException())
end

end

```

```julia
module FileCache

import Cache

function Cache.cache(data)
  # do cache stuff
end

end

```

```julia
include("Cache.jl")
include("FileCache.jl")

# use caching

```

However, Julia complains that `FileCache.cache` overwrites `Cache.cache` and breaks pre-compilation. Having different method signatures fixes the problem, but I don’t want to invent artificial function signatures for interface methods just for this purpose.

What’s the correct way to do it?

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [January 25, 2020, 12:17pm UTC](https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778/2 "2020-01-25T12:17:14Z")

</div>

Going through some code I’ve seen from other authors, I think the solution is to pass a type for dispatching and specializing methods, ie:

```julia
module Cache

function cache()
  throw(NotImplementedException())
end

end

```

```julia
module FileCache

function cache(::Cache{FileCache}, data)
  # stuff
end

end

```

Is this the (only) way?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 25, 2020, 12:22pm UTC](https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778/3 "2020-01-25T12:22:29Z")

</div>

You can define a function with no methods:

```julia
function cache end

```

in the `Cache` module.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [January 25, 2020, 12:54pm UTC](https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778/4 "2020-01-25T12:54:32Z")

</div>

Ah, that’s brilliant, thanks!

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [January 25, 2020, 2:42pm UTC](https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778/5 "2020-01-25T14:42:34Z")

</div>

> [@essenciary](#):
>
> ```
> function cache(data) 
> throw(NotImplementedException()) 
> end
> 
> ```

An aside, after working with a lot of code that onces used this kind of `NotImplementedException`,  
we concluded it was an antipattern.  
and replaced it with declaring the functin in the

```julia
function cache end

```

form.

- Plus a docstring saying what the espected signature was.
- Plus a test-suite function which accepts an implementation of the interface in the docstring and tests that it follows the interface.

Its great.  
Strong recommend,.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [January 25, 2020, 4:31pm UTC](https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778/6 "2020-01-25T16:31:31Z")

</div>

Excellent! It felt too much like Java anyway!

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [January 26, 2020, 8:42am UTC](https://discourse.julialang.org/t/design-pattern-for-correctly-overwriting-abstract-methods-interface-methods/33778/7 "2020-01-26T08:42:22Z")

</div>

the really big downside of the `NotImplementedException` is that if you do somethig like

```julia
function train(::AbstractModel, data) 
    throw(NotImplementedException()) 
end

struct GoodModel <: AbstractModel
...
end

function train(mdl::GoodModel, data::Vector) 
...
end

```

then call  
`train(mdl, (1,2,3))` (a tuple rather than a vector)  
then you get a `NotImplementedException` with a uninformative error message.

Rather than a `MethodError` listing what you called, vs what exists
