Skip to content

Commit ea3ca3b

Browse files
committed
treat Pair as broadcast scalar
1 parent c630477 commit ea3ca3b

File tree

4 files changed

+7
-2
lines changed

4 files changed

+7
-2
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Standard library changes
3333
(environment, flags, working directory, etc) if `x` is the first interpolant and errors
3434
otherwise ([#24353]).
3535
* `IPAddr` subtypes now behave like scalars when used in broadcasting ([#32133]).
36+
* `Pair` is now treated as a scalar for broadcasting ([#32209]).
3637
* `clamp` can now handle missing values ([#31066]).
3738

3839
#### Libdl

base/broadcast.jl

+1-1
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

+1-1
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

+4
Original file line numberDiff line numberDiff line change
@@ -822,3 +822,7 @@ let a = rand(5), b = rand(5), c = copy(a)
822822
x[[1,1]] .+= 1
823823
@test x == [2]
824824
end
825+
826+
# treat Pair as scalar:
827+
@test replace.(split("The quick brown fox jumps over the lazy dog"), r"[aeiou]"i => "_") ==
828+
["Th_", "q__ck", "br_wn", "f_x", "j_mps", "_v_r", "th_", "l_zy", "d_g"]

0 commit comments

Comments
 (0)