# Adding new method, with new dispatch pattern for an existing library function

**URL:** <https://discourse.julialang.org/t/adding-new-method-with-new-dispatch-pattern-for-an-existing-library-function/66110>\
**Category:** New to Julia\
**Tags:** question, functions\
**Created:** [August 10, 2021, 6:23am UTC](https://discourse.julialang.org/t/adding-new-method-with-new-dispatch-pattern-for-an-existing-library-function/66110 "2021-08-10T06:23:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [August 10, 2021, 6:23am UTC](https://discourse.julialang.org/t/adding-new-method-with-new-dispatch-pattern-for-an-existing-library-function/66110/1 "2021-08-10T06:23:03Z")

</div>

I’m extending the ‘interface’ of `ZipFile.Reader()` so it will understand `FilePathsBase.PosixPath` as an argument. **Is my solution properly idiomatic in Julia?**

```julia
using ZipFile, FilePathsBase
ZipFile.Reader(pp::PosixPath) = ZipFile.Reader(string(pp))

typeof(archive_file_path)
PosixPath

z = ZipFile.Reader(archive_file_path)
z_by_filename = Dict( f.name => f for f in z.files)

```

Seems to work nicely…

```julia
Dict{String, ZipFile.ReadableFile} with 16 entries:
  "verbatim.txt" => ZipFile.ReadableFile(name=verbatim.txt, method=D…
  "dataset/1bc719fd-c4e1-4… => ZipFile.ReadableFile(name=dataset/1bc719fd-c4e1-…
  "dataset/aae308f4-9f9c-4… => ZipFile.ReadableFile(name=dataset/aae308f4-9f9c-…
  "multimedia.txt" => ZipFile.ReadableFile(name=multimedia.txt, method…
  "dataset/b6465deb-2071-4… => ZipFile.ReadableFile(name=dataset/b6465deb-2071-…
  "dataset/50c9509d-22c7-4… => ZipFile.ReadableFile(name=dataset/50c9509d-22c7-…

```

Cheers…

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 10, 2021, 7:26am UTC](https://discourse.julialang.org/t/adding-new-method-with-new-dispatch-pattern-for-an-existing-library-function/66110/2 "2021-08-10T07:26:03Z")

</div>

You’re kind of losing all advantages of having `pp` be a `PosixPath` in the first place, by converting it to a `String`. Under the hood, `ZipFile.jl` seems to [just open the file directly anyway](https://github.com/fhs/ZipFile.jl/blob/3493e0030057b4c5d2c84e5b23ff872e9daa33a2/src/ZipFile.jl#L113-L119), so I’d do

```julia
ZipFile.Reader(pp::PosixPath) = ZipFile.Reader(open(pp))

```

instead, which dispatches to `ZipFile.Reader(::IO)` and saves you from creating a new `String` (which will allocate).

Another option would be investigating how to make `ZipFile.jl` more aware of `FilePathsBase.jl` and its `AbstractPath`, to make it work in general without having to define a forwarding method for each subtype of `AbstractPath`.

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [August 10, 2021, 7:38am UTC](https://discourse.julialang.org/t/adding-new-method-with-new-dispatch-pattern-for-an-existing-library-function/66110/3 "2021-08-10T07:38:54Z")

</div>

Now I get what you said - thanks!  
Yes it seemed like an awful hack for the type-system.

Which is what the compiler was trying to tell me with : ` ZipFile.Reader(::IO)`

```julia
MethodError: no method matching ZipFile.Reader(::PosixPath)
Closest candidates are:
  ZipFile.Reader(::AbstractString) at /home/peterg/.julia/packages/ZipFile/fdYkP/src/ZipFile.jl:117
  ZipFile.Reader(::IO) at /home/peterg/.julia/packages/ZipFile/fdYkP/src/ZipFile.jl:113
  ZipFile.Reader(::IO, ::Bool) at /home/peterg/.julia/packages/ZipFile/fdYkP/src/ZipFile.jl:103

```

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [August 10, 2021, 7:52am UTC](https://discourse.julialang.org/t/adding-new-method-with-new-dispatch-pattern-for-an-existing-library-function/66110/4 "2021-08-10T07:52:45Z")

</div>

It’s week 3 of me using Julia - so I didn’t feel quite ready to create a pull on someone’s library. But maybe I should go through the process on a local `dev` copy…  
I’ve seen how to do it on an example, but not built anything with it…

Julia library management seems really nice…
