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

Minimal support for dependent case classes #21698

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

smarter
Copy link
Member

@smarter smarter commented Oct 3, 2024

This lets us write:

    trait A:
      type B

    case class CC(a: A, b: a.B)

Pattern matching works but isn't dependent yet:

    x match
      case CC(a, b) =>
        val a1: A = a
        // Dependent pattern matching is not currently supported
        // val b1: a1.B = b
        val b1 = b // Type is CC#a.B

(for my usecase this isn't a problem, I'm working on a type constraint API which lets me write things like case class CC(a: Int, b: Int GreaterThan[a.type]))

Because case class pattern matching relies on the product selectors _N, making it dependent is a bit tricky, currently we generate:

    case class CC(a: A, b: a.B):
      def _1: A = a
      def _2: a.B = b

So the type of _2 is not obviously related to the type of _1, we probably need to change what we generate into:

    case class CC(a: A, b: a.B):
      @uncheckedStable def _1: a.type = a
      def _2: _1.B = b

But this can be done in a separate PR.

Fixes #8073.

@noti0na1
Copy link
Member

noti0na1 commented Oct 3, 2024

I am doing some experiment with dependent pattern match in CC, and this PR is useful for me as well!

I modified the desuger so _1, _2, etc will have better types: always use singleton types when the field is not var. I haven't finished because I also need to modify the typer so the selections in the cases are binded to something.

@smarter smarter force-pushed the i8073 branch 2 times, most recently from 83c5e98 to 8d1c7fb Compare October 3, 2024 20:06
@smarter
Copy link
Member Author

smarter commented Oct 3, 2024

@noti0na1 Awesome! Let me know how it goes.

@smarter smarter self-assigned this Oct 3, 2024
@smarter smarter force-pushed the i8073 branch 3 times, most recently from b3b5331 to 3f3ce18 Compare October 5, 2024 13:04
This used to fail with:
    trait <refinement> in value x extends enum EC, but extending enums is prohibited.
This lets us write:

    trait A:
      type B

    case class CC(a: A, b: a.B)

Pattern matching works but isn't dependent yet:

    x match
      case CC(a, b) =>
        val a1: A = a
        // Dependent pattern matching is not currently supported
        // val b1: a1.B = b
        val b1 = b // Type is CC#a.B

(for my usecase this isn't a problem, I'm working on a type constraint API
which lets me write things like `case class CC(a: Int, b: Int
GreaterThan[a.type])`)

Because case class pattern matching relies on the product selectors `_N`, making
it dependent is a bit tricky, currently we generate:

    case class CC(a: A, b: a.B):
      def _1: A = a
      def _2: a.B = b

So the type of `_2` is not obviously related to the type of `_1`, we probably
need to change what we generate into:

    case class CC(a: A, b: a.B):
      @uncheckedStable def _1: a.type = a
      def _2: _1.B = b

But this can be done in a separate PR.

Fixes scala#8073.
@odersky
Copy link
Contributor

odersky commented Oct 7, 2024

I think it's probably better to wait after the cutoff with this?

@smarter
Copy link
Member Author

smarter commented Oct 7, 2024

So 3.6.1 rather than 3.6.0?

@odersky
Copy link
Contributor

odersky commented Oct 7, 2024

Yes, 3.6.1 should work OK.

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

Successfully merging this pull request may close these issues.

Implement parameter dependent case classes
3 participants