# Is it type-piracy to define \`Base.parse(::Type{Tuple}, str)\`?

**URL:** <https://discourse.julialang.org/t/is-it-type-piracy-to-define-base-parse-type-tuple-str/112229>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 28, 2024, 11:30am UTC](https://discourse.julialang.org/t/is-it-type-piracy-to-define-base-parse-type-tuple-str/112229 "2024-03-28T11:30:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [March 28, 2024, 11:30am UTC](https://discourse.julialang.org/t/is-it-type-piracy-to-define-base-parse-type-tuple-str/112229/1 "2024-03-28T11:30:43Z")

</div>

type piracy is a thing in julia. I am wondering whether or under which circumstances it is fine to define something like

```julia
Base.parse(::Type{Tuple}, str) = ...

```

i.e. a Base function for a Base Type.

Or should you better define your own `MyPackage.parse` which falls back to `Base.parse`?

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [March 28, 2024, 11:32am UTC](https://discourse.julialang.org/t/is-it-type-piracy-to-define-base-parse-type-tuple-str/112229/2 "2024-03-28T11:32:23Z")

</div>

You own whether type nor function, so I’d say: yes, that’s piracy.

Edit: it may still be fine, though!

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [March 28, 2024, 12:15pm UTC](https://discourse.julialang.org/t/is-it-type-piracy-to-define-base-parse-type-tuple-str/112229/3 "2024-03-28T12:15:27Z")

</div>

because of what might it be fine?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [March 28, 2024, 12:20pm UTC](https://discourse.julialang.org/t/is-it-type-piracy-to-define-base-parse-type-tuple-str/112229/4 "2024-03-28T12:20:35Z")

</div>

> [@schlichtanders](#):
>
> Or should you better define your own `MyPackage.parse` which falls back to `Base.parse`?

I’d argue that this the better solution in all cases. The main problem with defining `Base.parse(::Type{Tuple}, str)` is that it is a global change that affects _all code everywhere_. So it might cause a lot recompilation of already compiled code and that code might work incorrectly afterwards. It could break code in some other package that relies on some particular behavior of `Base.parse` that you alter. It is essentially impossible to ensure that you don’t cause unwanted side effects.

Defining your own `MyPackage.parse` has none of these effects.

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [March 28, 2024, 12:22pm UTC](https://discourse.julialang.org/t/is-it-type-piracy-to-define-base-parse-type-tuple-str/112229/5 "2024-03-28T12:22:15Z")

</div>

It might be fine if you are pirating in private code and you can accept the consequences (invalidations, unexpected side effects, because you might overwrite someone else’s (also pirated!) method, etc.!).

If you commit type piracy in a public package that is supposed to form the basis of an ecosystem… that’s another story (don’t do it 🙂 )

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [March 28, 2024, 5:41pm UTC](https://discourse.julialang.org/t/is-it-type-piracy-to-define-base-parse-type-tuple-str/112229/6 "2024-03-28T17:41:05Z")

</div>

> [@vettert](#):
>
> It might be fine if you are pirating in private code and you can accept the consequences (invalidations, unexpected side effects, because you might overwrite someone else’s (also pirated!) method, etc.!).

It’s a bad idea in general. For one thing, changes to Base can make the pirated method ambiguous, so your once-good code will crash on an upgrade. This is a risk even without piracy, but piracy makes it more likely.

It’s easy to define your own `parse` function which falls back to `Base.parse`, or the equivalent for whatever function you’d like to extend. This will never become ambiguous, because your function will always be called on your code, so any new method in the package (doesn’t have to be Base or a stdlib) which would otherwise lead to ambiguity will never be called in the first place.

In other words, piracy isn’t (just) antisocial, it’s actively bad for your program. Shadowing a method is easy, do that instead, it’s always a better idea.
