# Change type from Any to actual types

**URL:** <https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663>\
**Category:** General Usage\
**Created:** [December 11, 2020, 1:48pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663 "2020-12-11T13:48:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojua8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cojua8/32/11882_2.png) [@cojua8](https://discourse.julialang.org/u/cojua8)\
**Post date:** [December 11, 2020, 1:48pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/1 "2020-12-11T13:48:49Z")

</div>

Hi,

I have an array of strings, which are replaced by ones or missings

```julia
julia> my_array = ["a", "b"]
2-element Array{String,1}:
"a"
"b"   

julia> my_replaced_array = replace(my_array, "a" => true, "b" => missing)
2-element Array{Any,1}:
true
missing 

```

but as you can see, the element type is now Any, instead of the desired Union{Missing, Bool}.

However, if I call an operation:

```julia
julia> .!my_replaced_array
2-element Array{Union{Missing, Bool},1}:
false
missing

```

The return type is the expected.

My question is if there is a way to get the right type of the elements of the replaced array from the elements of the array itself

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 11, 2020, 2:53pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/2 "2020-12-11T14:53:48Z")

</div>

One option is to convert after the replacement:

```julia
julia> my_replaced_array = Vector{Union{Bool,Missing}}(replace(my_array, "a" => true, "b" => missing))
2-element Array{Union{Missing, Bool},1}:
 true
     missing

```

I’m not sure it is the most efficient approach, though.

---

<div class="post-metadata">

**Author:** ![cojua8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cojua8/32/11882_2.png) [@cojua8](https://discourse.julialang.org/u/cojua8)\
**Post date:** [December 11, 2020, 3:03pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/3 "2020-12-11T15:03:26Z")

</div>

I’m looking for something more robust, which can work with any types.

But yeah, I think that is the solution for real cases, no need to develop a general case solution

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [December 11, 2020, 3:10pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/4 "2020-12-11T15:10:44Z")

</div>

You can try this trick

```julia
identity.(replace(my_array, "a" => true, "b" => missing))

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 11, 2020, 8:56pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/5 "2020-12-11T20:56:31Z")

</div>

Feels a bit clunky but seems to work:

```julia
julia> function replace2(x, pairs...)
         types = [pairs[j][2] for j = 1 : length(pairs)]
         T = eltype(types); 
         Vector{T}(replace(x, pairs...)); 
       end
replace2 (generic function with 1 method)
julia> replace2(my_array, "a" => true, "b" => missing)
2-element Array{Union{Missing, Bool},1}:
 true
     missing

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 11, 2020, 9:02pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/6 "2020-12-11T21:02:16Z")

</div>

```julia
julia> collect([true,missing])
2-element Vector{Union{Missing, Bool}}:
 true
     missing

```

how about this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 11, 2020, 10:18pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/7 "2020-12-11T22:18:18Z")

</div>

Note that `[true, missing]` is already of that element type:

```julia
julia> [true,missing]
2-element Array{Union{Missing, Bool},1}:
 true
     missing

```

and that `collect` doesn’t actually do the type narrowing:

```julia
julia> collect(Any[true,missing])
2-element Array{Any,1}:
 true
     missing

```

The `identity.(...)` “trick” is probably the most concise.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 12, 2020, 12:31am UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/8 "2020-12-12T00:31:41Z")

</div>

This has come up before, though I can’t find the exact threads. It might be worth having something in `Base` which takes an arbitrary array and converts it into the type that one would get from `[a, b, c]`

```julia

julia> t = Any[1, 2, 3]
3-element Array{Any,1}:
 1
 2
 3

julia> collect(t)
3-element Array{Any,1}:
 1
 2
 3

julia> identity.(t)
3-element Array{Int64,1}:
 1
 2
 3

julia> t = Any[1, 2, 3, 5.6]
4-element Array{Any,1}:
 1
 2
 3
 5.6

julia> identity.(t)
4-element Array{Real,1}:
 1
 2
 3
 5.6

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 12, 2020, 1:25pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/9 "2020-12-12T13:25:12Z")

</div>

However, `identity` does not seem to work in all cases. For example:

```julia
julia> identity.([true, 1])
2-element Array{Int64,1}:
 1
 1

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 12, 2020, 1:32pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/10 "2020-12-12T13:32:44Z")

</div>

Note that the `[` syntax already does type promotion there, ie

```julia
julia> [true, 1]
2-element Array{Int64,1}:
 1
 1

```

so `identity.(...)` is a no-op. But if you are a bit more careful about inputs, you will see that

```julia
julia> identity.(Any[1, true])
2-element Array{Integer,1}:
    1
 true

```

works fine.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 12, 2020, 1:47pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/11 "2020-12-12T13:47:01Z")

</div>

I guess what “works” means is ambiguous here. I expected the return type to be `Vector{Union{Bool,Int64}}` instead of `Vector{Int}`. But, yes, that’s a good point.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 12, 2020, 2:01pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/12 "2020-12-12T14:01:18Z")

</div>

> [@hendri54](#):
>
> I expected the return type to be `Vector{Union{Bool,Int64}}` instead of `Vector{Int}` .

But, if you read carefully, you will see that you got `Vector{Integer}`, not `Vector{Int}`. `Integer` is a supertype for both `Int` and `Bool`.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 12, 2020, 5:12pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/13 "2020-12-12T17:12:37Z")

</div>

Right - so it depends on whether the expected return type should be a Union of concrete types or their common supertype. Or is there a reason why the answer should always be the latter?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 12, 2020, 5:23pm UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/14 "2020-12-12T17:23:19Z")

</div>

The solution is to do a pass-through of the data first using `mapreduce(typeof, promote_typejoin, x)` and then fill in an array with that type, I think.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 13, 2020, 9:48am UTC](https://discourse.julialang.org/t/change-type-from-any-to-actual-types/51663/15 "2020-12-13T09:48:38Z")

</div>

> [@hendri54](#):
>
> whether the expected return type should be a Union of concrete types or their common supertype. Or is there a reason why the answer should always be the latter?

I don’t think that there is a solution that is generally the preferred one — it depends on various circumstances. Eg if I know there are a “few” concrete types, I would prefer `Union`, but if there are many, I would hope that the implementation gives up sooner.
