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

Recommend to use tuples to scalarize items in broadcast expressions #35591

Merged
merged 7 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/refpointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ There is no invalid (NULL) `Ref` in Julia, but a `C_NULL` instance of `Ptr` can
a `ccall` Ref argument.

# Use in broadcasting
`Ref` is sometimes used in broadcasting in order to treat the referenced values as a scalar:

Broadcasting with `Ref(x)` treats `x` as a scalar:
MasonProtter marked this conversation as resolved.
Show resolved Hide resolved
```jldoctest
julia> isa.(Ref([1,2,3]), [Array, Dict, Int])
3-element BitArray{1}:
Expand Down
13 changes: 12 additions & 1 deletion doc/src/manual/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ julia> convert.(Float32, [1, 2])
1.0
2.0

julia> ceil.((UInt8,), [1.2 3.4; 5.6 6.7])
julia> ceil.(UInt8, [1.2 3.4; 5.6 6.7])
2×2 Array{UInt8,2}:
0x02 0x04
0x06 0x07
Expand All @@ -945,6 +945,17 @@ julia> string.(1:3, ". ", ["First", "Second", "Third"])
"3. Third"
```

Sometimes, you want a container (like an array) that would normally participate in broadcast to be "protected"
from broadcast's behavior of iterating over all of its elements. By placing it inside another container
(like a single element [`Tuple`](@ref)) broadcast will treat it as a single value.
```jldoctest
julia> ([1, 2, 3], [4, 5, 6]) .+ ([1, 2, 3],)
([2, 4, 6], [5, 7, 9])

julia> ([1, 2, 3], [4, 5, 6]) .+ tuple([1, 2, 3])
([2, 4, 6], [5, 7, 9])
```

## Implementation

The base array type in Julia is the abstract type [`AbstractArray{T,N}`](@ref). It is parameterized by
Expand Down