# Base.replace issue with type promotion

**URL:** <https://discourse.julialang.org/t/base-replace-issue-with-type-promotion/21484>\
**Category:** General Usage\
**Created:** [March 5, 2019, 1:44am UTC](https://discourse.julialang.org/t/base-replace-issue-with-type-promotion/21484 "2019-03-05T01:44:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [March 5, 2019, 1:44am UTC](https://discourse.julialang.org/t/base-replace-issue-with-type-promotion/21484/1 "2019-03-05T01:44:12Z")

</div>

If I pass a function to `replace` then it doesn’t seem to promote the type of the vector.

```julia
julia> x = rand([nothing, 1,2,3], 10000);

julia> foo(v) = replace(x -> x === nothing ? missing : x, v)
foo (generic function with 1 method)

julia> foo(x)
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type Int64
Closest candidates are:
  convert(::Type{T<:Number}, ::T<:Number) where T<:Number at number.jl:6
  convert(::Type{T<:Number}, ::Number) where T<:Number at number.jl:7
  convert(::Type{T<:Integer}, ::Ptr) where T<:Integer at pointer.jl:23
  ...
Stacktrace:
 [1] convert(::Type{Union{Nothing, Int64}}, ::Missing) at ./some.jl:22
 [2] setindex!(::Array{Union{Nothing, Int64},1}, ::Missing, ::Int64) at ./array.jl:767
 [3] _replace!(::getfield(Main, Symbol("##15#16")), ::Array{Union{Nothing, Int64},1}, ::Array{Union{Nothing, Int64},1}, ::Int64) at ./set.jl:628
 [4] #replace#251 at ./set.jl:572 [inlined]
 [5] replace at ./set.jl:572 [inlined]
 [6] foo(::Array{Union{Nothing, Int64},1}) at ./REPL[32]:1
 [7] top-level scope at none:0

```

Since then I’ve figured a better way to use the `replace` function but I am still curious why the above usage fails.

```julia
julia> bar(v) = replace(v, nothing => missing)

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 5, 2019, 4:53am UTC](https://discourse.julialang.org/t/base-replace-issue-with-type-promotion/21484/2 "2019-03-05T04:53:46Z")

</div>

The output container `eltype` is easier to deduce for the `Pair`-based signature so special handling has been added to `Base` for that. For the version of `replace` which takes an arbitrary function this is more difficult and `Base` simply calls `similar` but this results in the bug you’ve noticed.

For your original version, you can just use `map` instead of replace which works correctly. The only difference seems to be the presence of the `count` keyword.

```julia
foo(v) = map(x -> x === nothing ? missing : x, v)

```
