|
| 1 | +//> using options -language:experimental.betterMatchTypeExtractors |
| 2 | + |
| 3 | +trait Expr: |
| 4 | + type Value |
| 5 | +object Expr: |
| 6 | + type Of[V] = Expr { type Value = V } |
| 7 | + type ExtractValue[F <: Expr] = F match |
| 8 | + case Expr.Of[v] => v |
| 9 | +import Expr.ExtractValue |
| 10 | + |
| 11 | +class SimpleLoop1 extends Expr: |
| 12 | + type Value = ExtractValue[SimpleLoop2] |
| 13 | + |
| 14 | +class SimpleLoop2 extends Expr: |
| 15 | + type Value = ExtractValue[SimpleLoop1] |
| 16 | + |
| 17 | +object Test1: |
| 18 | + val x: ExtractValue[SimpleLoop1] = 1 // error |
| 19 | + |
| 20 | +trait Description: |
| 21 | + type Elem <: Tuple |
| 22 | + |
| 23 | +class PrimBroken extends Expr: |
| 24 | + type Value = Alias |
| 25 | + type Alias = Value // error |
| 26 | + |
| 27 | +class Prim extends Expr: |
| 28 | + type Value = BigInt |
| 29 | + |
| 30 | +class VecExpr[E <: Expr] extends Expr: |
| 31 | + type Value = Vector[ExtractValue[E]] |
| 32 | + |
| 33 | +trait ProdExpr extends Expr: |
| 34 | + val description: Description |
| 35 | + type Value = Tuple.Map[description.Elem, [X] =>> ExtractValue[X & Expr]] |
| 36 | + |
| 37 | + |
| 38 | +class MyExpr1 extends ProdExpr: |
| 39 | + final val description = new Description: |
| 40 | + type Elem = (VecExpr[Prim], MyExpr2) |
| 41 | + |
| 42 | +class MyExpr2 extends ProdExpr: |
| 43 | + final val description = new Description: |
| 44 | + type Elem = (VecExpr[VecExpr[MyExpr1]], Prim) |
| 45 | + |
| 46 | +trait Constable[E <: Expr]: |
| 47 | + def lit(v: ExtractValue[E]): E |
| 48 | +object Constable: |
| 49 | + given [E <: Expr]: Constable[E] = ??? |
| 50 | + |
| 51 | +object Test2: |
| 52 | + def fromLiteral[E <: Expr : Constable](v: ExtractValue[E]): E = |
| 53 | + summon[Constable[E]].lit(v) |
| 54 | + val x0: ExtractValue[Prim] = "" // error |
| 55 | + val x1: ExtractValue[PrimBroken] = 1 // error |
| 56 | + |
| 57 | + val foo: MyExpr2 = new MyExpr2 |
| 58 | + val v: foo.Value = (Vector(Vector()), 1) // error: Recursion limit exceeded |
| 59 | + val c: MyExpr2 = fromLiteral: |
| 60 | + (Vector(Vector()), 1) // error: Recursion limit exceeded |
0 commit comments