# Type piracy?

**URL:** <https://discourse.julialang.org/t/type-piracy/9713>\
**Category:** General Usage\
**Tags:** type-piracy\
**Created:** [March 14, 2018, 11:45am UTC](https://discourse.julialang.org/t/type-piracy/9713 "2018-03-14T11:45:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 14, 2018, 11:45am UTC](https://discourse.julialang.org/t/type-piracy/9713/1 "2018-03-14T11:45:33Z")

</div>

I have the following function that takes a tuple and converts it to an unsigned integer:

```julia
function f(::Type{UInt16}, x::NTuple{2, UInt8})
    x1 = 0x0001 * x[1]
    x2 = 0x0100 * x[2]
    x1 + x2
end

```

What would be the right name for `f`? It used to be `Base.convert`, but I think that fits the definition of type piracy, `Base.reinterpret` probably too. I named it `to_unsigned` for now and removed the first argument.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 14, 2018, 12:26pm UTC](https://discourse.julialang.org/t/type-piracy/9713/2 "2018-03-14T12:26:03Z")

</div>

Can’t you encode your `x::NTuple{2, UInt8}` in your own type:

```julia
struct TwoUInt8
x1::UInt8
x2::UInt8
end

```

and then define the appropriate `Base.convert` method? That avoids type piracy.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 14, 2018, 1:06pm UTC](https://discourse.julialang.org/t/type-piracy/9713/3 "2018-03-14T13:06:13Z")

</div>

that is not really an option here, because I have tuples of various lengths and want to make UInts from them. Haven’t thought about declaring my own type though, I could simply declare `const NBytes{N} = NTuple{N, UInt8} where N`.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 14, 2018, 1:28pm UTC](https://discourse.julialang.org/t/type-piracy/9713/4 "2018-03-14T13:28:37Z")

</div>

> [@gdkrmr](#):
>
> Haven’t thought about declaring my own type though, I could simply declare const NBytes{N} = NTuple{N, UInt8} where N

That wouldn’t solve the type piracy, that’s just an alias

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 14, 2018, 1:32pm UTC](https://discourse.julialang.org/t/type-piracy/9713/5 "2018-03-14T13:32:22Z")

</div>

If you’re using a type alias, it’s still type piracy if you’re extending a function from Base.

I kind of like `mauro3`’s idea. Why is having tuples of various lengths a problem? You could just parameterize the type on `N`.

Just a wild thought: what you’re doing is kind of like `Base.read`, so you could do something like

```julia
struct StaticByteStream{N}
    tup::NTuple{N, UInt8}
end

Base.read(s::StaticByteStream{2}, ::Type{UInt16}) = ...

```

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [March 14, 2018, 1:34pm UTC](https://discourse.julialang.org/t/type-piracy/9713/6 "2018-03-14T13:34:49Z")

</div>

Another option would be to just call your function `combine`.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 14, 2018, 1:40pm UTC](https://discourse.julialang.org/t/type-piracy/9713/7 "2018-03-14T13:40:43Z")

</div>

I was just toying around with this and found the following weird performance issue (Julia Version 0.7.0-DEV.4570):

```julia
function f(::Type{UInt16}, x::NTuple{2, UInt8})
    x1 = 0x0001 * x[1]
    x2 = 0x0100 * x[2]
    x1 + x2
end

const NBytes{N} = NTuple{N, UInt8}
function f(::Type{UInt16}, x::NBytes{2})
    x1 = 0x0001 * x[1]
    x2 = 0x0100 * x[2]
    x1 + x2
end

julia> @btime f(UInt16, (0x01, 0x02))
  0.017 ns (0 allocations: 0 bytes)
0x0201

julia> x = NBytes((0x01, 0x02))
(0x01, 0x02)

julia> @btime f(UInt16, $x)
  1.690 ns (0 allocations: 0 bytes)
0x0201

```

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 14, 2018, 1:42pm UTC](https://discourse.julialang.org/t/type-piracy/9713/8 "2018-03-14T13:42:37Z")

</div>

Seeing that there is no one best solution I will probably give it a separate name.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 14, 2018, 1:49pm UTC](https://discourse.julialang.org/t/type-piracy/9713/9 "2018-03-14T13:49:06Z")

</div>

The “fast” one isn’t doing anything. The compiler can see the input and just optimized everything out.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 14, 2018, 5:37pm UTC](https://discourse.julialang.org/t/type-piracy/9713/10 "2018-03-14T17:37:07Z")

</div>

I tried several versions of this function and it is pretty impressive what the compiler can optimize out in Julia 0.7 compared to 0.6.

Why does reinterpret not work on tuples, for me this would seem like the most obvious choice for something like this?

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 17, 2018, 5:14pm UTC](https://discourse.julialang.org/t/type-piracy/9713/11 "2018-03-17T17:14:36Z")

</div>

Is there a zero overhead way to do this the other way round? the best I could come up with is the following:

```julia
function to_byte_tuple(x::T) where T <: Unsigned
    ntuple(i -> (x >> (8 * (i - 1))) % UInt8, sizeof(T))
end

```
