# Dispatching on struct fields

**URL:** <https://discourse.julialang.org/t/dispatching-on-struct-fields/18579>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 11, 2018, 11:03pm UTC](https://discourse.julialang.org/t/dispatching-on-struct-fields/18579 "2018-12-11T23:03:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [December 11, 2018, 11:03pm UTC](https://discourse.julialang.org/t/dispatching-on-struct-fields/18579/1 "2018-12-11T23:03:03Z")

</div>

Hello everyone, I am writing two libraries (let’s say A and B) which should work together, but ideally either should know about other’s existence. I thought I had worked everything out by making use of multiple-dispatch with `struct` fields, but seems it works differently as I expected. The essential example looks as follows:

```julia
module TestModule

struct Foo
    bar
end

testhello(foo::Foo) = hello(foo.bar)

end

struct Buzz
    bizz
end

hello(buzz::Buzz) = println("$(buzz.bizz)")

mybuzz = Buzz("Hello")
# Test hello
hello(mybuzz)
# Test testhello
TestModule.testhello(mybuzz)

```

Is it an expected behaviour or a bug?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 11, 2018, 11:06pm UTC](https://discourse.julialang.org/t/dispatching-on-struct-fields/18579/2 "2018-12-11T23:06:34Z")

</div>

`TestModule` doesn’t know about the `hello` function which is defined outside of it.

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [December 11, 2018, 11:30pm UTC](https://discourse.julialang.org/t/dispatching-on-struct-fields/18579/3 "2018-12-11T23:30:23Z")

</div>

Yes, that is the problem. I expeceted that since the `TestModule` would know about the type `Buzz` the methods for that type would follow. Is it appropriate to add another field to struct for letting `TestModule` to know about the `hello` method:

```julia
module TestModule

struct Foo
    bar
    hello
end

hello() = nothing 

testhello(foo::Foo) = foo.hello(foo.bar)

end

struct Buzz
    bizz
end

hello(buzz::Buzz) = println("$(buzz.bizz)")

mybuzz = Buzz("Hello")
# Test hello
hello(mybuzz)
# Test testhello
myfoo = TestModule.Foo(mybuzz,hello)
TestModule.testhello(myfoo)

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 11, 2018, 11:33pm UTC](https://discourse.julialang.org/t/dispatching-on-struct-fields/18579/4 "2018-12-11T23:33:46Z")

</div>

No, you’d typically want to import or export a `hello` function either from `TestModule` or into `TestModule`.
