# Abstract type functions

**URL:** <https://discourse.julialang.org/t/abstract-type-functions/30113>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 20, 2019, 5:25pm UTC](https://discourse.julialang.org/t/abstract-type-functions/30113 "2019-10-20T17:25:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peterd/32/10910_2.png) [@PeterD](https://discourse.julialang.org/u/PeterD)\
**Post date:** [October 20, 2019, 5:25pm UTC](https://discourse.julialang.org/t/abstract-type-functions/30113/1 "2019-10-20T17:25:18Z")

</div>

With the new Julia 1.3 release candidates out, I’m trying to take advantage of defining a function on an abstract type. However struggling big time to figure out how to leverage it when also using modules.

The below example goes wrong because the expression “l::Person)(msg)= greeting(l, msg)” is defined within the module Dummy and cannot see the greeting implementation outside Dummy. So it gets a “greeting not defined” error (if I don’t use modules, everything works as expected).

Any ideas are welcome how to workaround this.

```julia
module Dummy

export Person
abstract type Person end

(p::Person)(msg)= greeting(p, msg)

end

```

```julia
using .Dummy

struct RealPerson <: Person
    name
end

greeting(p::RealPerson, msg) = println(msg, p.name)

person = RealPerson("Peter")
person("Hello ")

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 20, 2019, 7:09pm UTC](https://discourse.julialang.org/t/abstract-type-functions/30113/2 "2019-10-20T19:09:59Z")

</div>

This isn’t really about the use of an abstract type; it’s just following the rules for all functions and methods in Julia (see [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/) ).

You need to somehow indicate that the `greeting` function you’re calling in Dummy is the _same_ function you’re defining outside of it. The fact that they happen to have the same name is not enough.

In this case, you can declare the function inside `Dummy` and implement a new method for it outside:

```julia
module Dummy

function greeting
end

...
end

using .Dummy
...
Dummy.greeting(p::RealPerson, msg) = ...

```

If you don’t want to write out `Dummy.greeting(`, you can do:

```julia
import .Dummy: greeting
greeting(p::RealPerson, msg) = ...

```

---

<div class="post-metadata">

**Author:** ![PeterD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peterd/32/10910_2.png) [@PeterD](https://discourse.julialang.org/u/PeterD)\
**Post date:** [October 20, 2019, 7:39pm UTC](https://discourse.julialang.org/t/abstract-type-functions/30113/3 "2019-10-20T19:39:40Z")

</div>

Thanks, that clarifies it!!!

And indeed you’re right, this is not specific to functions on abstract types. I guess just first time I encountered it with one of my own modules while just coincidentally I was playing with abstract types.
