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

Pattern match not return matched F-bounded type #11375

Open
yaqiz01 opened this Issue Jan 21, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@yaqiz01
Copy link

yaqiz01 commented Jan 21, 2019

I'm trying this example in scala 2.12.8

class B[N<:B[N]] { def foo(x:N) = println(s"$x") } 
class A extends B[A] 
def bar[N<:B[N]](n:N, v:A) = { n match { case n:A => n.foo(v); case _ => } }

The above example gives folllowing error

error: type mismatch;
 found   : this.A
 required: _1
def bar[N<:B[N]](n:N, v:A) = { n match { case n:A => n.foo(v); case _ => } }
                                                           ^

However, this compiles

def bar[N<:B[N]](n:N, v:A) = { n match { case n:A => n.asInstanceOf[A].foo(v); case _ => } }

Why does not n have type A after matched to A. This only occurs when N in B has bound N<:B[N].

@ashwinbhaskar

This comment has been minimized.

Copy link

ashwinbhaskar commented Jan 28, 2019

@yaqiz01 : interesting..But I don't think this is a fault in pattern matching. The below code gives the same error
def bar[N<:B[N]](n:N, v:A) = n.foo(v)

@SethTisue

This comment has been minimized.

Copy link
Member

SethTisue commented Jan 28, 2019

@ashwinbhaskar I think the compiler is correct to reject your variant. you should be able to arrive at that conclusion by reasoning (the compiler has been given no evidence that N and A are related, except via their common super type B); but as further support, note that Dotty accepts Yaqi's code but rejects your variant.

@SethTisue SethTisue added the patmat label Jan 28, 2019

@SethTisue SethTisue added this to the Backlog milestone Jan 28, 2019

@SethTisue SethTisue changed the title Pattern match not return matched type Pattern match not return matched F-bounded type Jan 28, 2019

@ashwinbhaskar

This comment has been minimized.

Copy link

ashwinbhaskar commented Jan 29, 2019

@SethTisue :Got it:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment