# Even more clarification on Type piracy

**URL:** <https://discourse.julialang.org/t/even-more-clarification-on-type-piracy/9079>\
**Category:** New to Julia\
**Tags:** package, type, type-piracy\
**Created:** [February 15, 2018, 11:14am UTC](https://discourse.julialang.org/t/even-more-clarification-on-type-piracy/9079 "2018-02-15T11:14:42Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [February 19, 2018, 9:46pm UTC](https://discourse.julialang.org/t/even-more-clarification-on-type-piracy/9079/22 "2018-02-19T21:46:51Z")

</div>

Clarification: if I don’t export a “pirated” function/type, is that ok? An example: I’m building a package to do stuff with temperature, and using Unitful all over the place. But I want to give users the option of just calling all of the functions that take temperatures with numbers instead.

Can I do:

```julia
import Base.convert
convert(::Type{Temperature}, x::Real) = # something that turns it into temperature

```

(Note: `Temperature` isn’t actually the real type, but I’m on mobile so forgive me please)

I know I could explicitly define extra methods that take numbers as arguments, but that seems needlessly verbose. If I don’t re-export convert, it won’t actually contaminate other people’s code right?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 19, 2018, 10:28pm UTC](https://discourse.julialang.org/t/even-more-clarification-on-type-piracy/9079/23 "2018-02-19T22:28:20Z")

</div>

> [@kevbonham](#):
>
> Clarification: if I don’t export a “pirated” function/type, is that ok? An example: I’m building a package to do stuff with temperature, and using Unitful all over the place. But I want to give users the option of just calling all of the functions that take temperatures with numbers instead.

No it’s not. See my example above for how this still (secretly) effects other codes.

`Base.convert` is a single function. As long as one library (`Base`) reexports it, it exists out there in the wild. You mutated it by adding a method. The one out there in the REPL is now mutated. That’s why type-piracy is so bad.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [February 19, 2018, 11:26pm UTC](https://discourse.julialang.org/t/even-more-clarification-on-type-piracy/9079/24 "2018-02-19T23:26:03Z")

</div>

I might call

```julia
Base.:*(a::Float64,b::Float64) = PackageANumber(a*b)

```

_method_ piracy. You have _not_ extended a function you don’t own on types that you don’t own. Rather you’ve _overwritten_ an existing method in `Base`. (or at least stolen an existing dispatch). You have commandeered something that is already in use. It’s obviously a semi-insane thing to do if you expect anyone else to use your package.

On the other hand, for one of the scenarios mentioned above, I prefer a term that Tony Kelman used: _type squatting_. It’s more evocative of the problem. For instance:

```julia
julia> :x * :y
ERROR: MethodError: no method matching *(::Symbol, ::Symbol)
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:424

julia> Base.:*(a::Symbol,b::Symbol) = :($a * $b)

julia> :x * :y
:(x * y)

```

If the package containing this code becomes widely used, then I’ve prevented julia, ie `Base` and the standard library, from extending its own function on its own types. I’m occupying something that was not being used. And evicting me could be difficult when that something needs to be used.

But, type squatting also causes an immediate problem with existing code. Namely, it may prevent an exception from being thrown as soon as possible after a function receives unsupported input data.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [February 20, 2018, 2:18am UTC](https://discourse.julialang.org/t/even-more-clarification-on-type-piracy/9079/25 "2018-02-20T02:18:19Z")

</div>

Huh… I thought you had to explicitly re-export the function to put the new method into scope when someone does `using`… Good to know.

---

<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:** [January 6, 2022, 10:47pm UTC](https://discourse.julialang.org/t/even-more-clarification-on-type-piracy/9079/26 "2022-01-06T22:47:32Z")

</div>

Here is an example of not re-exporting.

```julia
julia> module A
         export c
         *(a,b) = 4
         c = 3 * 3
       end
Main.A

julia> using Main.A

julia> c
4

julia> 3 * 3
9

```

Without the `Base.`, the overwriting does not propagate

[Previous page](https://discourse.julialang.org/t/even-more-clarification-on-type-piracy/9079.md?page=1)
