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

Honoring constraint on parametric type's parameter in UnionAll type #40952

Open
wsshin opened this issue May 26, 2021 · 2 comments
Open

Honoring constraint on parametric type's parameter in UnionAll type #40952

wsshin opened this issue May 26, 2021 · 2 comments
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@wsshin
Copy link
Contributor

wsshin commented May 26, 2021

Consider the following definition of a parametric type named MyType:

julia> VERSION
v"1.6.1-pre.1"

julia> struct MyType{R<:Real}
           x::R
       end

Now, we get

julia> (MyType{R} where {R<:Real}) <: MyType
true

but

julia> (MyType{R} where {R}) <: MyType
false

Because the parameter R of MyType must be a subtype of Real by definition, it will be nice if the last code returns true.

@mbauman
Copy link
Member

mbauman commented Oct 30, 2024

Beyond just "nice to have," this commonly surfaces as an unexpected method sorting because it means ::MyType{R} where {R} is less specific than just ::MyType. Cf. #6383 (comment) (but this is now the more relevant issue):

julia> struct A{T <: Real}; end

julia> f(::A{T}) where {T} = T
f (generic function with 1 method)

julia> f(::A, args...) = "surprise"
f (generic function with 2 methods)

julia> f(A{Int}())
"surprise"

julia> struct B{T}; end

julia> f(::B{T}) where {T} = T
f (generic function with 3 methods)

julia> f(::B, args...) = "no surprise"
f (generic function with 4 methods)

julia> f(B{Int}())
Int64

julia> methods(f)
# 4 methods for generic function "f" from Main:
 [1] f(::B{T}) where T
     @ REPL[6]:1
 [2] f(::B, args...)
     @ REPL[7]:1
 [3] f(::A, args...)
     @ REPL[3]:1
 [4] f(::A{T}) where T
     @ REPL[2]:1

@nsajko
Copy link
Contributor

nsajko commented Oct 31, 2024

Firstly, I'd like to note that this isn't specific to UnionAll, it also applies to struct, mutable struct, and abstract type.

Secondly, perhaps we just need better documentation, as asked for in #53380? The fact is, implementing this suggestion would make the language slightly less expressive, or it could risk introducing other gotchas.

One problem is that a solution to this issue would necessarily require some special-casing, because typeintersect is not in general exact.

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

No branches or pull requests

3 participants