Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace function doesn't work on Tuples #33782

Closed
oschulz opened this issue Nov 7, 2019 · 2 comments · Fixed by #38216
Closed

replace function doesn't work on Tuples #33782

oschulz opened this issue Nov 7, 2019 · 2 comments · Fixed by #38216

Comments

@oschulz
Copy link
Contributor

oschulz commented Nov 7, 2019

Just ran into this by chance: replace() seems broken for Tuples. It accepts a Tuple, but the generic implementation can't handle it:

julia> replace([1,2,3,4], 3 => 7)
4-element Array{Int64,1}:
 1
 2
 7
 4

julia> replace((1,2,3,4), 3 => 7)
ERROR: MethodError: no method matching similar(::NTuple{4,Int64}, ::Type{Int64})
[...]

Might be useful to add a specialized replace(::Tuple, ...) at some point (gen func for high performance?).

@ali-ramadhan
Copy link

Do we expect replace() to work for tuples?

Still an issue on

Julia Version 1.5.1
Commit 697e782ab8 (2020-08-25 20:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)

@ali-ramadhan
Copy link

Do we just need to define something like

_similar_or_copy(x::Tuple) = copy(x)

so that the following works?

julia/base/set.jl

Lines 555 to 564 in 8a52ea4

function replace(A, old_new::Pair...; count::Union{Integer,Nothing}=nothing)
V = promote_valuetype(old_new...)
if count isa Nothing
T = promote_type(subtract_singletontype(eltype(A), old_new...), V)
replace_pairs!(_similar_or_copy(A, T), A, typemax(Int), old_new)
else
U = promote_type(eltype(A), V)
replace_pairs!(_similar_or_copy(A, U), A, check_count(count), old_new)
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants