-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimal support for dependent case classes
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.
- Loading branch information
Showing
4 changed files
with
165 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import scala.deriving.Mirror | ||
|
||
trait A: | ||
type B | ||
|
||
case class CC(a: A, b: a.B) | ||
|
||
enum Enum: | ||
case EC(a: A, b: a.B) | ||
|
||
def test1(): Unit = | ||
val generic = summon[Mirror.Of[CC]] | ||
// No language syntax for type projection of a singleton type | ||
// summon[generic.MirroredElemTypes =:= (A, CC#a.B)] | ||
|
||
val dependent = summon[Mirror.Of[x.type]] | ||
summon[dependent.MirroredElemTypes =:= (A, x.a.B)] | ||
|
||
val aa: A { type B = Int } = new A { type B = Int } | ||
val x: CC { val a: aa.type } = CC(aa, 1).asInstanceOf[CC { val a: aa.type }] // manual `tracked` | ||
|
||
assert(CC(aa, 1) == generic.fromProduct((aa, 1))) | ||
assert(CC(aa, 1) == dependent.fromProduct((aa, 1))) | ||
|
||
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 | ||
|
||
end test1 | ||
|
||
def test2(): Unit = | ||
val generic = summon[Mirror.Of[Enum.EC]] | ||
// No language syntax for type projection of a singleton type | ||
// summon[generic.MirroredElemTypes =:= (A, Enum.EC#a.B)] | ||
|
||
val dependent = summon[Mirror.Of[x.type]] | ||
summon[dependent.MirroredElemTypes =:= (A, x.a.B)] | ||
|
||
val aa: A { type B = Int } = new A { type B = Int } | ||
val x: Enum.EC { val a: aa.type } = Enum.EC(aa, 1).asInstanceOf[Enum.EC { val a: aa.type }] // manual `tracked` | ||
|
||
assert(Enum.EC(aa, 1) == generic.fromProduct((aa, 1))) | ||
assert(Enum.EC(aa, 1) == dependent.fromProduct((aa, 1))) | ||
|
||
x match | ||
case Enum.EC(a, b) => | ||
val a1: A = a | ||
// Dependent pattern matching is not currently supported | ||
// val b1: a1.B = b | ||
val b1 = b // Type is Enum.EC#a.B | ||
|
||
end test2 | ||
|
||
@main def Test = | ||
test1() | ||
test2() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import scala.deriving.Mirror | ||
|
||
trait A: | ||
type B | ||
|
||
// Test local mirrors | ||
@main def Test = | ||
case class CC(a: A, b: a.B) | ||
|
||
enum Enum: | ||
case EC(a: A, b: a.B) | ||
|
||
def test1(): Unit = | ||
val generic = summon[Mirror.Of[CC]] | ||
// No language syntax for type projection of a singleton type | ||
// summon[generic.MirroredElemTypes =:= (A, CC#a.B)] | ||
|
||
val dependent = summon[Mirror.Of[x.type]] | ||
summon[dependent.MirroredElemTypes =:= (A, x.a.B)] | ||
|
||
val aa: A { type B = Int } = new A { type B = Int } | ||
val x: CC { val a: aa.type } = CC(aa, 1).asInstanceOf[CC { val a: aa.type }] // manual `tracked` | ||
|
||
assert(CC(aa, 1) == generic.fromProduct((aa, 1))) | ||
assert(CC(aa, 1) == dependent.fromProduct((aa, 1))) | ||
|
||
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 | ||
|
||
end test1 | ||
|
||
def test2(): Unit = | ||
val generic = summon[Mirror.Of[Enum.EC]] | ||
// No language syntax for type projection of a singleton type | ||
// summon[generic.MirroredElemTypes =:= (A, Enum.EC#a.B)] | ||
|
||
val dependent = summon[Mirror.Of[x.type]] | ||
summon[dependent.MirroredElemTypes =:= (A, x.a.B)] | ||
|
||
val aa: A { type B = Int } = new A { type B = Int } | ||
val x: Enum.EC { val a: aa.type } = Enum.EC(aa, 1).asInstanceOf[Enum.EC { val a: aa.type }] // manual `tracked` | ||
|
||
assert(Enum.EC(aa, 1) == generic.fromProduct((aa, 1))) | ||
assert(Enum.EC(aa, 1) == dependent.fromProduct((aa, 1))) | ||
|
||
x match | ||
case Enum.EC(a, b) => | ||
val a1: A = a | ||
// Dependent pattern matching is not currently supported | ||
// val b1: a1.B = b | ||
val b1 = b // Type is Enum.EC#a.B | ||
|
||
end test2 | ||
|
||
test1() | ||
test2() |