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

extractor for only possible concrete class of sealed trait considered refutable #18621

Open
bishabosha opened this issue Sep 29, 2023 · 3 comments

Comments

@bishabosha
Copy link
Member

bishabosha commented Sep 29, 2023

Compiler version

3.3.1

Minimized code

sealed trait Foo
final case class FooImpl(x: Int) extends Foo

def someFoo: Foo = FooImpl(23)
val FooImpl(myInt) = someFoo

Output

-- Warning: --------------------------------------------------------------------
5 |val FooImpl(myInt) = someFoo
  |                     ^^^^^^^
  |pattern's type FooImpl is more specialized than the right hand side expression's type Foo
  |
  |If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
  |which may result in a MatchError at runtime.
val myInt: Int = 23

Expectation

possibly be more sophisticated and accept when there is only one possible pattern?

@bishabosha bishabosha added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label itype:enhancement area:pattern-matching and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 29, 2023
@bishabosha bishabosha changed the title Single child of sealed trait considered refutable only possible pattern of sealed trait considered refutable Sep 29, 2023
@bishabosha
Copy link
Member Author

this did come from a test case, so I imagine its quite rare to have real-life code that really only has one child of a sealed trait.

@armanbilge
Copy link
Contributor

I just encountered this in http4s/http4s-crypto#179 :)

its quite rare to have real-life code that really only has one child of a sealed trait.

Perhaps. This strategy is useful to hide an implementation case class (that cannot be evolved binary-compatibly) behind a user-facing sealed trait. The codebase may internally prefer to operate on terms of the implementation class.

@bishabosha bishabosha changed the title only possible pattern of sealed trait considered refutable extractor for only possible concrete class of sealed trait considered refutable Oct 4, 2023
@bishabosha
Copy link
Member Author

perfect, we have the real world use case excellently put!

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

No branches or pull requests

2 participants