Skip to content

Commit 869e2a5

Browse files
mcabbottoxinabox
authored andcommitted
disallow arrays of symbols etc
1 parent ca247b3 commit 869e2a5

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/projection.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ _maybe_projector(x) = x
7373
_maybe_call(f::ProjectTo, x) = f(x)
7474
_maybe_call(f, x) = f
7575

76-
# Used for elements of e.g. Array{Any}, trivial projector
77-
_always_projector(x::Union{AbstractArray, Number, Ref}) = ProjectTo(x)
78-
_always_projector(x) = ProjectTo()
79-
8076
"""
8177
ProjectTo(x)
8278
@@ -164,11 +160,11 @@ end
164160

165161
# In other cases, store a projector per element:
166162
function ProjectTo(xs::AbstractArray)
167-
elements = map(_always_projector, xs)
163+
elements = map(ProjectTo, xs)
168164
if elements isa AbstractArray{<:ProjectTo{<:AbstractZero}}
169165
return ProjectTo{NoTangent}() # short-circuit if all elements project to zero
170-
elseif elements isa AbstractArray{<:ProjectTo{Any}}
171-
return ProjectTo{AbstractArray}(; element=ProjectTo(), axes=axes(xs)) # ... or all identity projection
166+
# elseif elements isa AbstractArray{<:ProjectTo{Any}}
167+
# return ProjectTo{AbstractArray}(; element=ProjectTo(), axes=axes(xs)) # ... or all identity projection
172168
else
173169
# Arrays of arrays come here, and will apply projectors individually:
174170
return ProjectTo{AbstractArray}(; elements=elements, axes=axes(xs))
@@ -208,7 +204,7 @@ function (project::ProjectTo{AbstractArray})(dx::Number) # ... so we restore fro
208204
end
209205

210206
# Ref -- works like a zero-array, also allows restoration from a number:
211-
ProjectTo(x::Ref) = ProjectTo{Ref}(; x = _always_projector(x[]))
207+
ProjectTo(x::Ref) = ProjectTo{Ref}(; x = ProjectTo(x[]))
212208
(project::ProjectTo{Ref})(dx::Ref) = Ref(project.x(dx[]))
213209
(project::ProjectTo{Ref})(dx::Number) = Ref(project.x(dx))
214210

test/projection.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ using OffsetArrays, BenchmarkTools
5656
@test !(y1 isa Adjoint) && !(y1[1] isa Adjoint)
5757

5858
# arrays of unknown things
59-
@test ProjectTo([:x, :y])(1:2) === 1:2 # no element handling,
60-
@test ProjectTo([:x, :y])(reshape(1:2,2,1,1)) == 1:2 # but still reshapes container
59+
@test_throws MethodError ProjectTo([:x, :y])
60+
@test_throws MethodError ProjectTo(Any[:x, :y])
6161

6262
@test ProjectTo(Any[1, 2])(1:2) == [1.0, 2.0] # projects each number.
6363
@test Tuple(ProjectTo(Any[1, 2+3im])(1:2)) === (1.0, 2.0 + 0.0im)

0 commit comments

Comments
 (0)