Skip to content

Commit

Permalink
Recommend to use tuples to scalarize items in broadcast expressions (J…
Browse files Browse the repository at this point in the history
…uliaLang#35591)

* don't wrap `UInt8` in a tuple

* Recommend to use tuples to scalarize an item.

* Don't recommend to use Ref for scalarizing

* Update arrays.md

* Update refpointer.jl

* add Matt's recommendation

* remove whitespace
  • Loading branch information
MasonProtter authored Apr 29, 2020
1 parent 8b010b5 commit 99faf1c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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:
```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

0 comments on commit 99faf1c

Please sign in to comment.