Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,17 @@ scalar_getindex(x::Tuple{<: Any}) = x[1]
end
end

eltype_exprs = [t <: Union{AbstractArray, Ref} ? :(eltype($t)) : :($t) for t ∈ a]
newtype_expr = :(return_type(f, Tuple{$(eltype_exprs...)}))
if isempty(exprs)
eltype_exprs = [t <: Union{AbstractArray, Ref} ? :(eltype($t)) : :($t) for t ∈ a]
newtype_expr = :(return_type(f, Tuple{$(eltype_exprs...)}))
else
newtype_expr = :(eltype(vals))
end

return quote
@_inline_meta
@inbounds return similar_type($first_staticarray, $newtype_expr, Size(newsize))(tuple($(exprs...)))
vals = tuple($(exprs...))
@inbounds return similar_type($first_staticarray, $newtype_expr, Size(newsize))(vals)
end
end

Expand Down
30 changes: 18 additions & 12 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,22 @@ end
@testset "eltype after broadcast" begin
# test cases issue #198
let a = SVector{4, Number}(2, 2.0, 4//2, 2+0im)
@test_broken eltype(a + 2) == Number
@test_broken eltype(a - 2) == Number
@test_broken eltype(a * 2) == Number
@test_broken eltype(a / 2) == Number
@test eltype(a + 2) == Number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these methods be deprecated? They are for general arrays.

julia> [1, 2, 3] + 1
ERROR: MethodError: no method matching +(::Array{Int64,1}, ::Int64)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
  +(::Complex{Bool}, ::Real) at complex.jl:292
  +(::Missing, ::Number) at missing.jl:93
  ...
Stacktrace:
 [1] top-level scope at none:0

julia> @SVector([1, 2, 3]) + 1
3-element SArray{Tuple{3},Int64,1,3}:
 2
 3
 4

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but orthogonal to this issue, except that I could use the explicit broadcast in these tests as I'm touching them anyways. (And it's in the broadcast tests.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, just wanted to point it out since I noticed it 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the tests locally; will push once I have feedback what to do about the empty case.

@test eltype(a - 2) == Number
@test eltype(a * 2) == Number
@test eltype(a / 2) == Number
end
let a = SVector{3, Real}(2, 2.0, 4//2)
@test_broken eltype(a + 2) == Real
@test_broken eltype(a - 2) == Real
@test_broken eltype(a * 2) == Real
@test_broken eltype(a / 2) == Real
@test eltype(a + 2) == Real
@test eltype(a - 2) == Real
@test eltype(a * 2) == Real
@test eltype(a / 2) == Real
end
let a = SVector{3, Real}(2, 2.0, 4//2)
@test_broken eltype(a + 2.0) == Float64
@test_broken eltype(a - 2.0) == Float64
@test_broken eltype(a * 2.0) == Float64
@test_broken eltype(a / 2.0) == Float64
@test eltype(a + 2.0) == Float64
@test eltype(a - 2.0) == Float64
@test eltype(a * 2.0) == Float64
@test eltype(a / 2.0) == Float64
end
let a = broadcast(Float32, SVector(3, 4, 5))
@test eltype(a) == Float32
Expand Down Expand Up @@ -196,4 +196,10 @@ end
d = SVector(1, 2, 3, 4)
@test_throws DimensionMismatch a .= d
end

@testset "issue #493" begin
X = rand(SVector{3,SVector{2,Float64}})
foo493(X) = normalize.(X)
@test foo493(X) isa Core.Compiler.return_type(foo493, Tuple{typeof(X)})
end
end