Open
Description
From the documentation:
replace(A, old_new::Pair...; [count::Integer])
Return a copy of collection
A
where, for each pairold=>new
inold_new
,
all occurrences ofold
are replaced bynew
.
Yet A
is not actually constrained to collections. This seems like a wasted opportunity to use Julia's type system to prevent bugs like the following.
x = 2.2
replace(x, NaN=>0)
ERROR: MethodError: no method matching similar(::Float64, ::Type{Float64})
Possible Solutions (Discussion)
- Somehow restrict the methods of
replace
to collections. I'm not sure if this is possible as there does not seem to be anAbstractCollection
that encompassesAbstractArray
,AbstractSet
,AbstractString
, etc. - Support non-collection values of
A
. While the example above could be done withifelse
or the ternary operator, replace could be very useful for non-iterable values ofA
. For example I often find myself wanting to write
(some function)
return replace(x, Inf=>NaN, -Inf=>NaN)
end