# Specializing a Unionall

**URL:** <https://discourse.julialang.org/t/specializing-a-unionall/15626>\
**Category:** General Usage\
**Created:** [September 28, 2018, 5:02pm UTC](https://discourse.julialang.org/t/specializing-a-unionall/15626 "2018-09-28T17:02:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimSheard](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@TimSheard](https://discourse.julialang.org/u/TimSheard)\
**Post date:** [September 28, 2018, 5:02pm UTC](https://discourse.julialang.org/t/specializing-a-unionall/15626/1 "2018-09-28T17:02:10Z")

</div>

I have two objects with similar type

julia\> typeof(price)  
PArr{Int64,PArr{Int64,Float64}}

julia\> typeof(flag)  
PArr{Int64,PArr{Int64,Char}}

I want to put them both in an array, so I use a unionall

qs = (PArr{Int,PArr{Int,w}} where {w})[price,flag];

I have a function that makes a copy of each

julia\> rs = share2(status,qs);

Now both the input and output have the same type

julia\> typeof(qs)  
Array{PArr{Int64,PArr{Int64,w}} where w,1}

julia\> typeof(rs)  
Array{PArr{Int64,PArr{Int64,w} where w},1}

But when I select an individual element from both, they have different types.

julia\> typeof(qs[1])  
PArr{Int64,PArr{Int64,Float64}}

julia\> typeof(rs[1])  
PArr{Int64,PArr{Int64,w} where w}

There must be someway of computing the type of rs[1], so that I can pass  
it to a function that demands PArr{Int64,PArr{Int64,Float64}},

---

<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:** [September 28, 2018, 5:39pm UTC](https://discourse.julialang.org/t/specializing-a-unionall/15626/2 "2018-09-28T17:39:02Z")

</div>

> [@TimSheard](#):
>
> Now both the input and output have the same type
> 
> julia\> typeof(qs)  
> Array{PArr{Int64,PArr{Int64,w}} where w,1}
> 
> julia\> typeof(rs)  
> Array{PArr{Int64,PArr{Int64,w} where w},1}

No they are not!  
One of them is an array of abstract eltype `qs` that are `PArr{Int64, PArr{Int64, w}}` where `w` can take any values. The other one is an array of concrete type `PArr{Int64, PArr{Int64, w} where w}`

---

<div class="post-metadata">

**Author:** ![TimSheard](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@TimSheard](https://discourse.julialang.org/u/TimSheard)\
**Post date:** [September 28, 2018, 5:42pm UTC](https://discourse.julialang.org/t/specializing-a-unionall/15626/3 "2018-09-28T17:42:51Z")

</div>

Yes, they are different types, But rs[1] is a value, so It has some concrete type.  
How do I compute that? A Unionall “Foo{x,w} where w” is an existential it says there exists a “w”, So how do I find out what “w” is?, and if “w” is what I expect (say Int) how do I cast the object to type Foo{x,Int} ?

---

<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:** [September 28, 2018, 5:47pm UTC](https://discourse.julialang.org/t/specializing-a-unionall/15626/4 "2018-09-28T17:47:01Z")

</div>

> [@TimSheard](#):
>
> But rs[1] is a value, so It has some concrete type.

`typeof(rs[1])`? Note that `rs` has a **concrete** eltype that is `PArr{Int64, PArr{Int64, w} where w}`, which is what you get in your `typeof(rs[1])` and that is already the type of `rs[1]`. I don’t know how you created it but you’ve done a convertion there, the type of `rs[1]` isn’t the same as `qs[1]` anymore.

> [@TimSheard](#):
>
> A Unionall “Foo{x,w} where w” is an existential it says there exists a “w”

So, correct, but that’s not what you get in `typeof(rs[1])`.

> [@TimSheard](#):
>
> and if “w” is what I expect (say Int) how do I cast the object to type Foo{x,Int} ?

Changing the type is conversion. Julia is strongly typed so you cannot just change the type of an object without creating a new one. What you use to do the conversion is object dependent (usually `convert` if it’s allowed).

---

<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:** [September 29, 2018, 9:10am UTC](https://discourse.julialang.org/t/specializing-a-unionall/15626/5 "2018-09-29T09:10:48Z")

</div>

(Aside, have a read of [https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530.](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530.))
