# Abstract methods: pattern

**URL:** <https://discourse.julialang.org/t/abstract-methods-pattern/44327>\
**Category:** General Usage\
**Created:** [August 5, 2020, 11:59am UTC](https://discourse.julialang.org/t/abstract-methods-pattern/44327 "2020-08-05T11:59:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 5, 2020, 11:59am UTC](https://discourse.julialang.org/t/abstract-methods-pattern/44327/1 "2020-08-05T11:59:33Z")

</div>

I want to create a “module general” which provides an abstract type (fruit), to be subtyped in another “module specific”. In the “module general”, I want to provide operations on fruit in general, but which hae to rely on methods specific for the concrete type.

```julia
 module general
     export fruit,weights
     abstract type fruit end
     function weight end
     weights(fruits) = [weight(fruit) for fruit in fruits]
 end
 module specific
     using general
     export apple,lemon,weight
     struct apple <: fruit
         taste
     end
     weight(f::apple)=0.2
     struct lemon <: fruit
         taste
     end
     weight(f::lemon)=0.1
 end
 using general,specific
 weights([apple(:delicious),lemon(:bitter)])

```

As must be expected, this does not work: in “module general” the weight methods are undefined. The example is simplistic, and provides no reason why I would like to keep things in separate modules, I must ask you to take this as a given.

**Is there any way I can, together, with the definition of an abstract class in “module general” provide functions that rely on using methods specific to subtypes, to be defined in another module?** Or is that not the Julian way?

😀

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [August 5, 2020, 12:26pm UTC](https://discourse.julialang.org/t/abstract-methods-pattern/44327/2 "2020-08-05T12:26:52Z")

</div>

Yes, that’s possible and widely used. You should explicitly add methods to `general.weight`:

```julia
general.weight(::apple)=0.2
general.weight(::lemon)=0.1

```

It is also possible to import `general.weight` into `specific`’s namespace, but that’s not recommended in order to avoid unintentional overloads.

Also, please follow the [naming guidelines](https://docs.julialang.org/en/v1/manual/style-guide/#Use-naming-conventions-consistent-with-Julia-base/-1) to make your code easier to read for others.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 5, 2020, 12:31pm UTC](https://discourse.julialang.org/t/abstract-methods-pattern/44327/3 "2020-08-05T12:31:12Z")

</div>

Vot tak, konietshno! I should have thought about it, that what “import” is all about.  
Thanks a lot, Vasily! And I will read the naming guidelines.
