From 18aa75faac29ac977cb1cf6fa69a67f258090823 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Tue, 16 Apr 2024 14:48:00 +0200 Subject: [PATCH] Fix method ambiguity error of `shuffle!` with `AbstractArray{Bool}` on Julia 1.11 (#24) Co-authored-by: Eric Hanson <5846501+ericphanson@users.noreply.github.com> --- src/StableRNGs.jl | 5 ++++- test/runtests.jl | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/StableRNGs.jl b/src/StableRNGs.jl index af62491..4145725 100644 --- a/src/StableRNGs.jl +++ b/src/StableRNGs.jl @@ -141,7 +141,10 @@ end # https://github.com/JuliaRandom/StableRNGs.jl/issues/10 Random.shuffle(r::StableRNG, a::AbstractArray) = Random.shuffle!(r, Base.copymutable(a)) -function Random.shuffle!(r::StableRNG, a::AbstractArray) +# Fix method ambiguity issue: https://github.com/JuliaRandom/StableRNGs.jl/issues/23 +Random.shuffle!(r::StableRNG, a::AbstractArray) = _shuffle!(r, a) +Random.shuffle!(r::StableRNG, a::AbstractArray{Bool}) = _shuffle!(r, a) +function _shuffle!(r::StableRNG, a::AbstractArray) require_one_based_indexing(a) n = length(a) n <= 1 && return a # nextpow below won't work with n == 0 diff --git a/test/runtests.jl b/test/runtests.jl index f7a9165..f1a6515 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -139,6 +139,14 @@ end b = collect(a) shuffle!(StableRNG(10), b) @test b == a_shuffled + + # https://github.com/JuliaRandom/StableRNGs.jl/issues/23 + c = [false, true, false, true, false] + c_shuffled = [false, false, false, true, true] + @test shuffle!(StableRNG(123), c) == c_shuffled + d = [false true; true false] + d_shuffled = [true true; false false] + @test shuffle(StableRNG(31), d) == d_shuffled end # https://github.com/JuliaRandom/StableRNGs.jl/issues/20