# Is this type piracy?

**URL:** <https://discourse.julialang.org/t/is-this-type-piracy/48894>\
**Category:** General Usage\
**Tags:** type-piracy\
**Created:** [October 23, 2020, 5:03pm UTC](https://discourse.julialang.org/t/is-this-type-piracy/48894 "2020-10-23T17:03:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [October 23, 2020, 5:03pm UTC](https://discourse.julialang.org/t/is-this-type-piracy/48894/1 "2020-10-23T17:03:22Z")

</div>

```julia
(::Type{T})(x::MyType) where T = dosomething(x) |> T

```

or perhaps just bad practice for some other reason?

This is basically hijacking all single argument constructors to first `dosomething` with `MyType`. This seemed like a convenient thing when I wrote it but now I’m having second thoughts.

In case context is needed, `MyType` is a `TensorProto` and `dosomething` is the method which figures out which field is used and reshapes and reinterprets it to the right shape and type. The above function gives the convenience that `Array(readproto("file.pb", TensorProto()))` and `CuArray(readproto("file.pb", TensorProto()))` just works.

I guess one drawback is that it could create a surprise for people who wants to wrap a `TensorProto` in some other struct, but if that is the only drawback I think it can be mitigated by just making `T <: AbstractArray` or just forcing people to overload for that type.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [October 23, 2020, 5:08pm UTC](https://discourse.julialang.org/t/is-this-type-piracy/48894/2 "2020-10-23T17:08:44Z")

</div>

It’s not type piracy, but it does cause a TON of method invalidations so it’s really bad for compiler latency.

And yes, you’re right that it’s likely a bad idea even if it’s not piracy and even if you don’t care about latency because it can lead to surprising and unexpected behaviour.

---

<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:** [October 23, 2020, 5:10pm UTC](https://discourse.julialang.org/t/is-this-type-piracy/48894/3 "2020-10-23T17:10:43Z")

</div>

If `dosomething` returns a `MyType`, you’ll get a stackoverflow:

```julia
julia> struct MyType end

julia> dosomething(x) = x
dosomething (generic function with 1 method)

julia> (::Type{T})(x::MyType) where T = dosomething(x) |> T

julia> Int(MyType())
ERROR: StackOverflowError:
Stacktrace:
 [1] |>(::MyType, ::Type{T} where T) at ./operators.jl:834
 [2] Int64(::MyType) at ./REPL[4]:1
 ... (the last 2 lines are repeated 39988 more times)

```

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [October 26, 2020, 7:09pm UTC](https://discourse.julialang.org/t/is-this-type-piracy/48894/4 "2020-10-26T19:09:26Z")

</div>

@Mason: Hehe, yeah. I haven’t learned to naturally think about invalidations yet, but I saw now that a late contender in the [invalidation competition](https://discourse.julialang.org/t/a-playful-competition-who-can-define-a-method-that-invalidates-the-most-code/47543/46) was more or less exactly this. Perhaps I can get a consolation prize for being the fool who actually put it in “real” code 🙂

@Sukera: That’s of course true and kinda contributes to the overall shakiness.
