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

Zero + StaticVector is ambiguous #18

Closed
cstjean opened this issue May 17, 2018 · 6 comments
Closed

Zero + StaticVector is ambiguous #18

cstjean opened this issue May 17, 2018 · 6 comments

Comments

@cstjean
Copy link
Collaborator

cstjean commented May 17, 2018

On master,

using StaticArrays, FillArrays
SVector(2,3) + Zeros(2)

@willtebbutt

@dlfivefifty
Copy link
Member

Here's the full error:

julia> SVector(2,3) + Zeros(2)
ERROR: MethodError: +(::StaticArrays.SArray{Tuple{2},Int64,1,2}, ::FillArrays.Zeros{Float64,1,Tuple{Int64}}) is ambiguous. Candidates:
  +(a::AbstractArray{T,N}, b::FillArrays.Zeros{V,N,SZ} where SZ) where {T, V, N} in FillArrays at /Users/sheehanolver/.julia/v0.6/FillArrays/src/FillArrays.jl:278
  +(a::StaticArrays.StaticArray, b::AbstractArray) in StaticArrays at /Users/sheehanolver/.julia/v0.6/StaticArrays/src/linalg.jl:12
Possible fix, define
  +(::StaticArrays.StaticArray{S,T,N} where S<:Tuple, ::FillArrays.Zeros{V,N,SZ} where SZ)

@dlfivefifty
Copy link
Member

Options:

  1. StaticArrays depends on FillArrays
  2. FillArrays depends on StaticArrays
  3. FillArrays only overrides concrete types, e.g. change to +(a::Array{T, N}, b::Zeros{V, N}) where {T, V, N}

I think we expect this to return an SVector and therefore be allocation-free.

@dlfivefifty
Copy link
Member

I'm going to do (3) for now and we can come back to this later.

@willtebbutt
Copy link
Contributor

Ah, yeah. I agree regarding possible solutions. 1 and 2 are only partial fixes though, as we'll keep running into this ambiguity with every other AbstractArray subtype that defines its own notion of addition with other AbstractArrays. I think option 3 might therefore be the best option -- it's a bit limiting but makes it quite unlikely that we'll encounter this ambiguity.

@dlfivefifty
Copy link
Member

This is a case where + in Base should be rewritten to use traits I think. A la JuliaLang/julia#25558

@cstjean
Copy link
Collaborator Author

cstjean commented May 17, 2018

FWIW, the StaticArrays method for addition is quite good already...

julia> @btime $(SVector(2,3)) + $(Zeros(2))
  5.028 ns (0 allocations: 0 bytes)
2-element StaticArrays.SArray{Tuple{2},Float64,1,2}:
 2.0
 3.0

julia> 

julia> @btime identity($(SVector(2,3)))
  2.025 ns (0 allocations: 0 bytes)
2-element StaticArrays.SArray{Tuple{2},Int64,1,2}:
 2
 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants