# Duck typing when \`quack\` is not in \`Base\`

**URL:** <https://discourse.julialang.org/t/duck-typing-when-quack-is-not-in-base/26011>\
**Category:** General Usage\
**Created:** [July 4, 2019, 1:38pm UTC](https://discourse.julialang.org/t/duck-typing-when-quack-is-not-in-base/26011 "2019-07-04T13:38:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lairez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lairez/32/9183_2.png) [@lairez](https://discourse.julialang.org/u/lairez)\
**Post date:** [July 4, 2019, 1:38pm UTC](https://discourse.julialang.org/t/duck-typing-when-quack-is-not-in-base/26011/1 "2019-07-04T13:38:43Z")

</div>

Say that I develop a package `Foo` that does interesting things with any type `T` with a well defined function `quack(T)`. Typically, I may not want to impose `T` to let the user add relevant metadata or use a type from another package.

```julia
module Foo

function foo(x)
   q = quack(x)
   # ... do something with q ...
end

end

```

It works in a very satisfactory way when `quack` is in `Base` (like arithmetic operations). The user extends `quack` for its own type and everything works. But when `quack` is not in `Base`, julia complains that `quack` is not defined. I can define it with something useless, but it is not very elegant.

```julia
module Foo

quack( :: Union{} ) = nothing

function foo(x)
   q = quack(x)
   # ... do something with q ...
end

end

```

Moreover, it is not _plug and play_: if another package `Bar` defines a type `S` with a function `quack`, `Foo` will not be able to use it directly, it will require plumbing.

Is there a customary solution in this situation?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 4, 2019, 1:52pm UTC](https://discourse.julialang.org/t/duck-typing-when-quack-is-not-in-base/26011/2 "2019-07-04T13:52:28Z")

</div>

> [@lairez](#):
>
> I can define it with something useless, but it is not very elegant.

Use `function quack end` to define the function `quack`.

> [@lairez](#):
>
> if another package `Bar` defines a type `S` with a function `quack` , `Foo` will not be able to use it directly, it will require plumbing.

It will need to depend on `Foo` and extend `Foo.quack`.

---

<div class="post-metadata">

**Author:** ![lairez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lairez/32/9183_2.png) [@lairez](https://discourse.julialang.org/u/lairez)\
**Post date:** [July 4, 2019, 2:02pm UTC](https://discourse.julialang.org/t/duck-typing-when-quack-is-not-in-base/26011/3 "2019-07-04T14:02:46Z")

</div>

> [@kristoffer.carlsson](#):
>
> Use `function quack end` to define the function `quack` .

Thanks, that’s better!

> [@kristoffer.carlsson](#):
>
> It will need to depend on `Foo` and extend `Foo.quack` .

I guess it has some good…

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [July 5, 2019, 6:43am UTC](https://discourse.julialang.org/t/duck-typing-when-quack-is-not-in-base/26011/4 "2019-07-05T06:43:01Z")

</div>

You might find [GitHub - JuliaData/DataAPI.jl: A data-focused namespace for packages to share functions](https://github.com/JuliaData/DataAPI.jl) interesting: it’s a way to solve this in the data ecosystem. One package (DataAPI.jl) defines the names and maybe default definitions, and various packages depend on this one to extend them. The idea being you might want a very lightweight package to provide the common names, rather than having many heavy packages depend on each other just in order to extent bits of functionality.
