# What is a safer way to expand other packages' function?

**URL:** <https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818>\
**Category:** General Usage\
**Tags:** function\
**Created:** [November 27, 2023, 8:10pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818 "2023-11-27T20:10:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)\
**Post date:** [November 27, 2023, 8:10pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/1 "2023-11-27T20:10:50Z")

</div>

Hi friends,

I am try to expand the DataFrame function for my data struct, I defined it as:

```julia
using DataFrames

DataFrame(Q::mystrust) = begin
# do something 
end

```

it works well, but when I write above as a file and include it in another session,

```julia
using DataFrames
include("my_function.jl")

```

DataFrame now only have 1 methods, and other function provided by package DataFrames seems be replaced.

```julia
>DataFrame
DataFrame (generic function with 1 method)

```

are there any suggestion for avoid replace other packages function while I expand them?  
Thanks for your time!!

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 27, 2023, 8:13pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/2 "2023-11-27T20:13:21Z")

</div>

You need the namespace: `DataFrames.DataFrame(::MyStruct) = ...`

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [November 27, 2023, 8:34pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/3 "2023-11-27T20:34:38Z")

</div>

Interesting. As a medium-term Julia user, I’m not sure I fully internalized this.

For some `PackageA.jl` with exported function `f`, my thought process would have been that `using PackageA` would bring `f` into the current namespace, then defining a new `f(::MyType)` would add a new method for `f` within the current namespace. It sounds like what you’re suggesting is that defining `f(::MyType)` instead overwrites the binding for `f` exported by `PackageA` with a new function possessing only a single method, essentially: `f` previously pointed to a methods table populated by `PackageA.f` but now points to a new methods table containing only to your local `f`. By comparison, defining it as `PackageA.f(::MyType)` clarifies that you’re intending to add a method to an existing table vs reassigning the local name.

Does that sound right?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 27, 2023, 8:41pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/4 "2023-11-27T20:41:49Z")

</div>

Yes

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 27, 2023, 8:53pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/5 "2023-11-27T20:53:42Z")

</div>

Yes, and you can also use an explicit import to add methods to the function of other package.

It it wasn’t that way, we would be all the time doing type piracy without knowing it, as every function we define would have to take into account all names exported by every used package, including Base.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 27, 2023, 8:58pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/7 "2023-11-27T20:58:30Z")

</div>

You could also import the function you want to extend.

```julia
import DataFrames: DataFrame

DataFrame(Q::mystrust) = begin
# do something 
end

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 27, 2023, 9:01pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/8 "2023-11-27T21:01:53Z")

</div>

> [@lmiq](#):
>
> Yes, and you can also use an explicit import to add methods to the function of other package.

> [@mkitti](#):
>
> You could also import the function you want to extend.

This is true but I don’t like it.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [November 27, 2023, 9:09pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/9 "2023-11-27T21:09:52Z")

</div>

> [@jar1](#):
>
> This is true but I don’t like it.

I for one prefer it. I get why it’s not good to extend exported names; if I remove a name from a module’s export statement, suddenly the remaining extension methods make a new function and the `using` statement won’t alert me to my slipup. However, I always found it strange that when a name is specified (so I can’t use that name for anything else), I need to qualify it in extension method names but nowhere else, not even the body. There’s the argument that it’s clearer when the global scope is split into separate modules, but exported names and other specified names aren’t qualified either, so I still have to start at the file with the `include`s and imports to be sure where names come from.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [November 27, 2023, 9:21pm UTC](https://discourse.julialang.org/t/what-is-a-safer-way-to-expand-other-packages-function/106818/10 "2023-11-27T21:21:23Z")

</div>

> [@lmiq](#):
>
> It it wasn’t that way, we would be all the time doing type piracy without knowing it, as every function we define would have to take into account all names exported by every used package, including Base.

This makes a lot of sense in hindsight. I’ve made a habit of extending existing functions with their global names, e.g. `Base.show(::MyType) = ...` but I suppose I hadn’t fully processed the why. I think I’d assumed it was something to do with providing method access to other non-locally-scoped code, rather, it’s that `show(::MyType) = ...` would just hijack the whole of the (non-globally-referenced) `show` in the local scope.
