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

Parametrised method not specialised correctly? #17023

Closed
dpsanders opened this issue Jun 20, 2016 · 3 comments
Closed

Parametrised method not specialised correctly? #17023

dpsanders opened this issue Jun 20, 2016 · 3 comments

Comments

@dpsanders
Copy link
Contributor

dpsanders commented Jun 20, 2016

The following definition of + for a Vol type representing an N-dimensional volume should (I believe) call the parametrised + method when the two Ns are the same; however, it calls the catch-all method that is designed for Vol objects with different N (EDIT: i.e. those not covered by the more restricted method definition):

immutable Vol{N,T<:Real}
    value::T   
end

Vol{T}(N, x::T) = Vol{N,T}(x)

import Base.+

+(V1::Vol, V2::Vol) = throw(ArgumentError("Volumes of different dimension cannot be added"))
+{N,T}(V1::Vol{N,T}, V2::Vol{N,T}) = Vol{N,T}(V1.value + V2.value)

V1 = Vol(3, 2)
V2 = Vol(3, 4)

julia> V1 + V2
ERROR: ArgumentError: Volumes of different dimension cannot be added
 in + at none:1
@dpsanders
Copy link
Contributor Author

It does, however, seemingly work correctly if the catch-all method is replaced by the unwieldy

+{N1,N2,T}(V1::Vol{N1,T}, V2::Vol{N2,T}) = throw(ArgumentError("Volumes of different dimension cannot be added"))

@JeffBezanson
Copy link
Member

I believe this is because Vol by itself has T<:Real, but in the parameterized method T is unrestricted. Try

+{N,T<:Real}(V1::Vol{N,T}, V2::Vol{N,T}) = Vol{N,T}(V1.value + V2.value)

@JeffBezanson
Copy link
Member

Dup of #6383

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

2 participants