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

NTuple as a type parameter is not invariant #6561

Closed
amitmurthy opened this issue Apr 18, 2014 · 6 comments
Closed

NTuple as a type parameter is not invariant #6561

amitmurthy opened this issue Apr 18, 2014 · 6 comments
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@amitmurthy
Copy link
Contributor

This used to work, doesn't now.

  | | |_| | | | (_| |  |  Version 0.3.0-prerelease+2657 (2014-04-18 03:17 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit ca44f9b (0 days old master)
|__/                   |  x86_64-linux-gnu

julia> function foo(args::Vector{Tuple})
       end
foo (generic function with 1 method)

julia> foo([(1,2), (3,4)])
ERROR: no method foo(Array{(Int64,Int64),1})

NTuple instead of Tuple dispatches fine.

@simonster
Copy link
Member

I'm not really sure this should work for either Tuple or NTuple, given that Julia's type parameters are invariant. Passing a Vector{Tuple} does work as expected:

julia> foo(convert(Vector{Tuple}, [(1,2), (3,4)]))

(although oddly Tuple[(1, 2), (3, 4)] throws an error)

@amitmurthy
Copy link
Contributor Author

I don't quite understand why an explicit convert is required. Isn't Array{(Int64,Int64),1} a vector of tuples?

@JeffBezanson
Copy link
Member

@simonster is quite right.

Tuple[...] isn't defined as an array constructor since tuples (and Tuple) are also indexable. Though it could perhaps be defined for unambiguous cases.

@amitmurthy
Copy link
Contributor Author

Any explanation of why it is working with NTuple will be helpful.

@JeffBezanson
Copy link
Member

Must be a bug.

@simonster simonster changed the title Error dispatching on Tuple "NTuple" type parameter is not invariant Apr 18, 2014
@Jutho
Copy link
Contributor

Jutho commented Apr 19, 2014

The following certainly explains your behaviour:

typeof([(1,2), (3,4)])  # returns  Array{(Int64,Int64),1} or thus Vector{(Int64,Int64)}
Vector{(Int64,Int64)} <: Vector{Tuple}  # returns false

and I do indeed remember from reading somewhere that it is not because T1 <: T2 that Array{T1} <: Array{T2}. However, I don't know how

Vector{(Int64,Int64)} <: Vector{NTuple}  # returns true

fits in this behaviour. I would think only the following strategy could work:

typealias TupleVector{N,T} Vector{NTuple{N,T}}
Vector{(Int64,Int64)} <: TupleVector # returns true

@simonster simonster changed the title "NTuple" type parameter is not invariant NTuple type parameter is not invariant Apr 19, 2014
@JeffBezanson JeffBezanson self-assigned this Apr 19, 2014
@JeffBezanson JeffBezanson changed the title NTuple type parameter is not invariant NTuple as a type parameter is not invariant Apr 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants