Skip to content

Commit 46c9a77

Browse files
authored
treat Pair as broadcast scalar (#32209)
1 parent 4324531 commit 46c9a77

File tree

4 files changed

+7
-2
lines changed

4 files changed

+7
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Standard library changes
4646
Previously, the functions `+`, `-`, `*`, `/`, `conj`, `real` and `imag` returned the unwrapped element
4747
when operating over zero-dimensional arrays ([#32122]).
4848
* `IPAddr` subtypes now behave like scalars when used in broadcasting ([#32133]).
49+
* `Pair` is now treated as a scalar for broadcasting ([#32209]).
4950
* `clamp` can now handle missing values ([#31066]).
5051
* `empty` now accepts a `NamedTuple` ([#32534]).
5152
* `mod` now accepts a unit range as the second argument to easily perform offset modular arithmetic to ensure the result is inside the range ([#32628]).

base/broadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ julia> Broadcast.broadcastable("hello") # Strings break convention of matching i
652652
Base.RefValue{String}("hello")
653653
```
654654
"""
655-
broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val,Ptr,Regex}) = Ref(x)
655+
broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val,Ptr,Regex,Pair}) = Ref(x)
656656
broadcastable(::Type{T}) where {T} = Ref{Type{T}}(T)
657657
broadcastable(x::Union{AbstractArray,Number,Ref,Tuple,Broadcasted}) = x
658658
# Default to collecting iterables — which will error for non-iterables

base/pair.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const => = Pair
2121
2222
Construct a `Pair` object with type `Pair{typeof(x), typeof(y)}`. The elements
2323
are stored in the fields `first` and `second`. They can also be accessed via
24-
iteration.
24+
iteration (but a `Pair` is treated as a single "scalar" for broadcasting operations).
2525
2626
See also: [`Dict`](@ref)
2727

test/broadcast.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,7 @@ let a = rand(5), b = rand(5), c = copy(a)
831831
x[[1,1]] .+= 1
832832
@test x == [2]
833833
end
834+
835+
# treat Pair as scalar:
836+
@test replace.(split("The quick brown fox jumps over the lazy dog"), r"[aeiou]"i => "_") ==
837+
["Th_", "q__ck", "br_wn", "f_x", "j_mps", "_v_r", "th_", "l_zy", "d_g"]

0 commit comments

Comments
 (0)