# Pluto.jl - How do I dispatch to a subtype created in a notebook?

**URL:** <https://discourse.julialang.org/t/pluto-jl-how-do-i-dispatch-to-a-subtype-created-in-a-notebook/124162>\
**Category:** General Usage\
**Tags:** question, dispatch, struct, pluto\
**Created:** [December 24, 2024, 11:01pm UTC](https://discourse.julialang.org/t/pluto-jl-how-do-i-dispatch-to-a-subtype-created-in-a-notebook/124162 "2024-12-24T23:01:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g-gundam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g-gundam/32/47593_2.png) [@g-gundam](https://discourse.julialang.org/u/g-gundam)\
**Post date:** [December 24, 2024, 11:01pm UTC](https://discourse.julialang.org/t/pluto-jl-how-do-i-dispatch-to-a-subtype-created-in-a-notebook/124162/1 "2024-12-24T23:01:27Z")

</div>

I created a small package called [ProblematicAnimals.jl](https://github.com/g-gundam/ProblematicAnimals.jl) to demonstrate a problem I’m having with Pluto.jl notebooks. I sincerely hope a solution to this can be found.

## Defined outside of Pluto

```julia
abstract type AbstractAnimal end
struct Cat <: AbstractAnimal end
sound(cat::Cat) = "meow"
why(a::AbstractAnimal) = sound(a)

```

## It should work like this.

```julia-repl
julia> cat = Cat()
Cat()

julia> sound(cat)
"meow"

julia> why(cat)
"meow"

```

Simple.

## However, I can’t get a subtype defined in a notebook to work.

From inside Pluto.jl:

```julia
struct Pig <: AbstractAnimal end

sound(pig::Pig) = "oink"

pig = Pig()

sound(pig) # "oink"

why(pig) # The `sound(pig::Pig)` function is invisible to the why(a) function.

```

- How can I get `why(pig)` to work in a Pluto.jl notebook?
- I’ve provided a [notebook](https://github.com/g-gundam/ProblematicAnimals.jl/blob/main/notebooks/dispatch.jl) that you can try for yourself in Pluto.jl to see the problem yourself.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [December 25, 2024, 1:12am UTC](https://discourse.julialang.org/t/pluto-jl-how-do-i-dispatch-to-a-subtype-created-in-a-notebook/124162/2 "2024-12-25T01:12:02Z")

</div>

The problem is the definition of the method `sound` in the notebook. See this section of the manual: [Modules · The Julia Language](https://docs.julialang.org/en/v1/manual/modules/#using-and-import-with-specific-identifiers,-and-adding-methods)

The code in the linked notebook defines a new function Main.sound rather than implementing a new method for ProblematicAnimals.sound. You have two choices to fix it, the easiest is to fully qualify the name when defining the new method.

```
ProblematicAnimals.sound(pig::Pig) = "oink"

```

The other is to import that method as described in the manual in the block at the top of the notebook.

---

<div class="post-metadata">

**Author:** ![g-gundam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g-gundam/32/47593_2.png) [@g-gundam](https://discourse.julialang.org/u/g-gundam)\
**Post date:** [December 25, 2024, 1:18am UTC](https://discourse.julialang.org/t/pluto-jl-how-do-i-dispatch-to-a-subtype-created-in-a-notebook/124162/3 "2024-12-25T01:18:30Z")

</div>

Thank you so much. That was a nice Christmas present.
