replace(::String,...) is a special case. You’ll find replace(1, 1=>999) doesn’t work either. Because what is the point of a replace method working on scalars - unless those scalars have some internal structure where substitution is meaningful like strings?
Okay. I had guessed this aspect. What I’m missing is understanding the reason (and I think there is a reason) why in the following two cases (where strings or string vectors are not involved but only missing and [missing] ) there is a behavior different
julia> replace(missing, "a" => "b")
ERROR: MethodError: no method matching similar(::Missing, ::Type{Any})
julia> replace([missing], "a" => "b")
1-element Vector{Union{Missing, String}}:
missing
Because the primary use of replace is to iterate a collection and replace any elements that equal e by s. replace([1, 2, 3], 1=>999) == [999, 2, 3]. You can’t do this with scalars because it doesn’t make sense to iterate them. Strings are an exception because they are also a collection of substrings.
I probably set the question wrong (but I wasn’t initially clear about the whole context).
What I wanted to clarify is what @pdeffebach pointed out
which I rewrite below so as to exclude the specifics of strings (scalar and iterable at the same time) from the question
replace doesn’t work on scalars, only collections. missing isn’t a collection, but strings are, in a sense.
Note in your examples of replace(::Vector{String}) vs replace.(::Vector{String}), that they both “work”, but they don’t do the same thing. The first one treats each string as a scalar, and the second as a collection, so the results are different.
mmmmh… maybe this clarifies my point better.
It’s not so much being misslingt that generates the error but being an immutable scalar(!?).
So if there was a passONE similar to passMISSING we would have this result…
using Missings
passmissing(replace)(missing,1=>999)
#hand made
using Ones
passone(replace)(1,2=>999)
1
at this point I wonder if it is correct that passmissing() changes this behavior in the case of missing
I mean yeah, but that has nothing to do with replace specifically. For instance, there is no sort(scalar) method (I assume, not at a computer right now), so sort(missing) is an error. But passmissing(sort)(missing) is missing. passmissing doesn’t care whether its argument function has a method for missing, if any of that argument function’s arguments is missing, passmissing returns missing.
Yes I have seen passmissing() do this.
I wonder if this isn’t a stretch in cases where, explicitly, the (wrapped) functions are not applicable to that specific situation (single missing and replace or sort functions , for example)
Return a function that returns missing if any of its positional arguments are
missing (even if their number or type is not consistent with any of the
methods defined for f) and otherwise applies f to these arguments.