Specializing a Unionall

I have two objects with similar type

julia> typeof(price)

julia> typeof(flag)

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])

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}},

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}

1 Like

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} ?

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.

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

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).

1 Like

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