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.