Skip to content

Commit f054f46

Browse files
authored
Unify broadcasting behavior of materialize!! (#35)
The broadcasting behavior of `materialize!!(dest, x)` is made consistent for mutable and immutable destinations `dest`. Previously, the shape and type of immutable `dest` was not considered. This commit changes NoBang.materialize to also let the `dest` participate in the broadcast, thereby propagating information about its shape and type.
1 parent bae8300 commit f054f46

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/NoBang/base.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ resize(xs::AbstractVector, n::Integer) = similar(xs, (n,))
149149

150150
setproperty(value, name, x) = setproperties(value, NamedTuple{(name,)}((x,)))
151151

152-
materialize(::Any, x) = Broadcast.materialize(x)
152+
materialize(dest, x) =
153+
Broadcast.materialize(Broadcast.instantiate(Broadcast.broadcasted(first tuple, x, dest)))
153154

154155
@inline _union(args...) = union(args...)
155156

src/core.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ implements(::typeof(setproperty!), ::Type{<:NamedTuple}) = false
8585

8686
struct Undefined end
8787
implements(::Mutator, ::Undefined) = false
88+
Base.broadcastable(x::Undefined) = Ref(x)
8889

8990
"""
9091
possible(f!, args...) :: Bool

test/preamble.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using Test
33
using Base: ImmutableDict
44
using BangBang
55
using BangBang: implements
6-
using StaticArrays: SVector
6+
using StaticArrays: SVector, SizedVector
77

88
"""
99
==ₜ(x, y)

test/test_macro.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ end
3333

3434
let x = [1, 2]
3535
y = SVector(0, 0)
36-
@test (@! y .= x .* 2)::Vector{Int} == [2, 4]
36+
@test (@! y .= x .* 2)::SizedVector{2, Int} == [2, 4]
37+
end
38+
39+
let y = [0, 0]
40+
@test (@! y .= 1)::Vector{Int} == [1, 1]
41+
end
42+
43+
let y = SVector(0, 0)
44+
@test (@! y .= 1) === SVector(1, 1)
3745
end
3846
end
3947

@@ -63,7 +71,15 @@ end
6371

6472
let x = [1, 2]
6573
y = SVector(0, 0)
66-
@test (@! @. y = x * 2)::Vector{Int} == [2, 4]
74+
@test (@! @. y = x * 2)::SizedVector{2, Int} == [2, 4]
75+
end
76+
77+
let y = [0, 0]
78+
@test (@! @. y = 1)::Vector{Int} == [1, 1]
79+
end
80+
81+
let y = SVector(0, 0)
82+
@test (@! @. y = 1) === SVector(1, 1)
6783
end
6884
end
6985

0 commit comments

Comments
 (0)