# Type Intersection in Arrays

**URL:** <https://discourse.julialang.org/t/type-intersection-in-arrays/12035>\
**Category:** General Usage\
**Created:** [June 28, 2018, 2:19pm UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035 "2018-06-28T14:19:59Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [June 28, 2018, 3:47pm UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/22 "2018-06-28T15:47:40Z")

</div>

Anyhow this is going off topic, but I’ll be happy to discuss this elsewhere

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [June 28, 2018, 5:04pm UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/23 "2018-06-28T17:04:54Z")

</div>

> [@pdeffebach](#):
>
> sorry i misread! but you could still do `t = Vector{typeof(x[1]}(x)`

This is actually nice when I _know_ that there is only one type. I’d need to check whether there is any performance gain though.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 28, 2018, 8:30pm UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/24 "2018-06-28T20:30:55Z")

</div>

Why not `[x...]` ?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [June 28, 2018, 8:38pm UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/25 "2018-06-28T20:38:24Z")

</div>

> [@dlfivefifty](#):
>
> Why not `[x...]` ?

By all means avoid splatting a collection with arbitrary length, unless you really know it can only have a few elements.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 28, 2018, 8:39pm UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/26 "2018-06-28T20:39:04Z")

</div>

But he already said he doesn’t care about speed…

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [June 28, 2018, 9:00pm UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/27 "2018-06-28T21:00:04Z")

</div>

I guess the splatting is maybe the idea that most explicitly captures that I want the type intersection.

how bad would the performance penalty be for - say - 1000 elements?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 29, 2018, 12:25pm UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/28 "2018-06-29T12:25:41Z")

</div>

On 0.7-beta it’s actually faster:

```julia
julia> x = Vector{Any}(randn(100_000)); @time [x...];
  0.001294 seconds (7 allocations: 1.526 MiB)

julia> @time identity.(x);
  0.004294 seconds (199.50 k allocations: 3.807 MiB)

```

EDIT: before someone complains about using `@time`:

```julia
julia> foo(x) = [x...]; @btime foo(x)
  427.769 μs (3 allocations: 1.53 MiB)

julia> @btime identity.(x);
  3.698 ms (199497 allocations: 3.81 MiB)

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [July 2, 2018, 8:21am UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/29 "2018-07-02T08:21:44Z")

</div>

The question isn’t really how fast it is for 100 entries, it’s more whether the number of entries can vary, in which case a new method will be compiled for each new number of entries. So you need to include the compilation time too, plus the memory cost of having these compiled methods in the cache.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [July 2, 2018, 8:40am UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/30 "2018-07-02T08:40:50Z")

</div>

Good point. Likely there is no serious penalty how I use this at the moment, but good to be aware.

How would _you_ solve this problem?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [July 2, 2018, 9:06am UTC](https://discourse.julialang.org/t/type-intersection-in-arrays/12035/31 "2018-07-02T09:06:28Z")

</div>

I guess we need a function like this:

```julia
function promote_entries(A::AbstractArray)
    T = mapreduce(typeof, promote_type, A)
    convert(Array{T}, A)
end

```

Currently this doesn’t seem to be optimized when `A` is homogeneous, which is a bit annoying but could easily be fixed by handling that situation manually.

[Previous page](https://discourse.julialang.org/t/type-intersection-in-arrays/12035.md?page=1)
