# Implementing a function from an interface, multiple dispatch

**URL:** <https://discourse.julialang.org/t/implementing-a-function-from-an-interface-multiple-dispatch/20694>\
**Category:** General Usage\
**Created:** [February 12, 2019, 6:46am UTC](https://discourse.julialang.org/t/implementing-a-function-from-an-interface-multiple-dispatch/20694 "2019-02-12T06:46:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [February 12, 2019, 6:46am UTC](https://discourse.julialang.org/t/implementing-a-function-from-an-interface-multiple-dispatch/20694/1 "2019-02-12T06:46:51Z")

</div>

I am reading the source of `CSVFiles.jl` and I see it implements the `TableTraits` interface, which in Julia, is the defacto way of describing table data. I see functions like

```julia
function TableTraits.getiterator(file::CSVFile)
    res = _loaddata(file)
    it = TableTraitsUtils.create_tableiterator([i for i in res[1]], [Symbol(i) for i in res[2]])
    return it
end 

```

I noticed `TableTraits.getiterator` rather than just `getiterator`. I am a bit confused on why the name is prepended with `TableTraits`. I thought multiple dispatch didn’t require this? And that it would pick the right `getiterator` based on the types it gets. In this particular case, if someone was to call `getiterator(mycsvfile::CSVFile)`, this function should be called. Do we need the prepend, and if so why?

Follow up question… suppose I have my own custom type and that I’d like a “pretty print” of this type in the repl. Would It not be sufficient to define `show(io, MyType)` or do I have to define `Base.show(io, MyType)`?

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [February 12, 2019, 7:45am UTC](https://discourse.julialang.org/t/implementing-a-function-from-an-interface-multiple-dispatch/20694/2 "2019-02-12T07:45:36Z")

</div>

In Julia, there may be many _functions_ with the same name in different modules, and each function may have multiple _methods_. As motivation for this, imagine 2 functions - Databases.connect(), which is responsible for connecting to various databases (via different methods that implement this function) and another function Graphs.connect() which is responsible for connecting nodes in a graph, and may also have multiple implementing methods.

Prepanding method name with a module name (or alternatively importing function name explicitely) tells Julia to extend existing function instead of creating a new one in current module.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 12, 2019, 8:07am UTC](https://discourse.julialang.org/t/implementing-a-function-from-an-interface-multiple-dispatch/20694/3 "2019-02-12T08:07:23Z")

</div>

define `Base.show(io::IO, x::MyType)`

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 12, 2019, 8:14am UTC](https://discourse.julialang.org/t/implementing-a-function-from-an-interface-multiple-dispatch/20694/4 "2019-02-12T08:14:03Z")

</div>

or, alternatively,

```julia
import Base: show

show(io::IO, x::MyType) = ...

```

However, I typically prefer `Base.show`.
