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

Seemingly a bug with type variables having Tuple as a lower bound #39277

Closed
julbinb opened this issue Jan 15, 2021 · 3 comments
Closed

Seemingly a bug with type variables having Tuple as a lower bound #39277

julbinb opened this issue Jan 15, 2021 · 3 comments
Labels
docs This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch

Comments

@julbinb
Copy link

julbinb commented Jan 15, 2021

(Julia 1.5.3 and 1.0.5)

When a type variable has a Tuple lower bound, scoping of the type variable and allowed uses seem wrong.

Allowed usage

julia> foo(a::T) where {T>:Tuple} = (a,)
foo (generic function with 1 method)

julia> foo(1) # seems wrong
(1,)

julia> foo((1,))
((1,),)

Scoping

julia> bar(a::T) where {T>:Tuple} = (a, T)
bar (generic function with 1 method)

julia> bar(1) # seems wrong
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] bar(::Int64) at ./REPL[1]:1
 [2] top-level scope at none:0

julia> bar((1,))
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] bar(::Tuple{Int64}) at ./REPL[1]:1
 [2] top-level scope at none:0

julia> baz(a::T) where {T>:Tuple{Int}} = (a, T)
baz (generic function with 1 method)

julia> baz(1)
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] baz(::Int64) at ./REPL[4]:1
 [2] top-level scope at none:0

julia> baz((1,))
((1,), Tuple{Int64})

Correct scoping:

julia> moo(a::T) where T = (a,T)
moo (generic function with 1 method)

julia> moo(5)
(5, Int64)

julia> moo((5,))
((5,), Tuple{Int64})

(Examples were made in cooperation with @BenChung)

@JeffBezanson
Copy link
Member

This is allowed because there is indeed a type greater than Tuple that includes 1, e.g. Any. But it's not clear how to uniquely determine such a type. We handle that situation by allowing the method call (since Tuple{Int} <: Tuple{>:Tuple} is true), but leaving T undefined inside. So the scope is correct, we just don't have a value for T.

@martinholters
Copy link
Member

Is there anything to be done here? Or can this be closed?

@julbinb
Copy link
Author

julbinb commented Jan 20, 2021

This is allowed because there is indeed a type greater than Tuple that includes 1, e.g. Any. But it's not clear how to uniquely determine such a type. We handle that situation by allowing the method call (since Tuple{Int} <: Tuple{>:Tuple} is true), but leaving T undefined inside. So the scope is correct, we just don't have a value for T.

Hm, I see, thanks. In this particular case, given T >: Int, T >: Tuple, the smallest T would be Union{Int, Tuple}, but in general I am not sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

5 participants