diff --git a/library/src-non-bootstrapped/scala/quoted/package.scala b/library/src-non-bootstrapped/scala/quoted/package.scala index aa724ad6f937..51e19c14f400 100644 --- a/library/src-non-bootstrapped/scala/quoted/package.scala +++ b/library/src-non-bootstrapped/scala/quoted/package.scala @@ -8,4 +8,13 @@ package object quoted { def withQuoteContext[T](thunk: given QuoteContext => T) given (toolbox: Toolbox): T = throw new Exception("Non bootsrapped library") + implicit object ExprOps { + def (x: T) toExpr[T: Liftable] given QuoteContext: Expr[T] = the[Liftable[T]].toExpr(x) + + def (seq: Seq[Expr[T]]) toExprOfSeq[T] given (tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = + throw new Exception("Non bootsrapped library") + + def (list: List[Expr[T]]) toExprOfList[T] given Type[T], QuoteContext: Expr[List[T]] = + throw new Exception("Non bootsrapped library") + } } diff --git a/library/src/scala/quoted/Liftable.scala b/library/src/scala/quoted/Liftable.scala index d26988d55008..1437407aad6f 100644 --- a/library/src/scala/quoted/Liftable.scala +++ b/library/src/scala/quoted/Liftable.scala @@ -1,5 +1,7 @@ package scala.quoted +import scala.reflect.ClassTag + /** A typeclass for types that can be turned to `quoted.Expr[T]` * without going through an explicit `'{...}` operation. */ @@ -43,4 +45,191 @@ object Liftable { } } + given [T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] { + def toExpr(iarray: IArray[T]): given QuoteContext => Expr[IArray[T]] = '{ + val array = new Array[T](${iarray.length.toExpr})(ClassTag(${the[ClassTag[T]].runtimeClass.toExpr})) + ${ Expr.block(List.tabulate(iarray.length)(i => '{ array(${i.toExpr}) = ${iarray(i).toExpr} }), '{ array.asInstanceOf[IArray[T]] }) } + } + } + + given [T: Type: Liftable] as Liftable[Seq[T]] = new Liftable[Seq[T]] { + def toExpr(xs: Seq[T]): given QuoteContext => Expr[Seq[T]] = + xs.map(the[Liftable[T]].toExpr).toExprOfSeq + } + + given [T: Type: Liftable] as Liftable[List[T]] = new Liftable[List[T]] { + def toExpr(xs: List[T]): given QuoteContext => Expr[List[T]] = + xs.map(the[Liftable[T]].toExpr).toExprOfList + } + + given [T: Type: Liftable] as Liftable[Set[T]] = new Liftable[Set[T]] { + def toExpr(set: Set[T]): given QuoteContext => Expr[Set[T]] = + '{ Set(${set.toSeq.toExpr}: _*) } + } + + given [T: Type: Liftable, U: Type: Liftable] as Liftable[Map[T, U]] = new Liftable[Map[T, U]] { + def toExpr(map: Map[T, U]): given QuoteContext => Expr[Map[T, U]] = + '{ Map(${map.toSeq.toExpr}: _*) } + } + + given [T: Type: Liftable] as Liftable[Option[T]] = new Liftable[Option[T]] { + def toExpr(x: Option[T]): given QuoteContext => Expr[Option[T]] = x match { + case Some(x) => '{ Some[T](${x.toExpr}) } + case None => '{ None: Option[T] } + } + } + + given [L: Type: Liftable, R: Type: Liftable] as Liftable[Either[L, R]] = new Liftable[Either[L, R]] { + def toExpr(x: Either[L, R]): given QuoteContext => Expr[Either[L, R]] = x match { + case Left(x) => '{ Left[L, R](${x.toExpr}) } + case Right(x) => '{ Right[L, R](${x.toExpr}) } + } + } + + given [T1: Type: Liftable] as Liftable[Tuple1[T1]] = new { + def toExpr(tup: Tuple1[T1]) = + '{ Tuple1(${tup._1.toExpr}) } + } + + given [T1: Type: Liftable, T2: Type: Liftable] as Liftable[Tuple2[T1, T2]] = new { + def toExpr(tup: Tuple2[T1, T2]) = + '{ (${tup._1.toExpr}, ${tup._2.toExpr}) } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable] as Liftable[Tuple3[T1, T2, T3]] = new { + def toExpr(tup: Tuple3[T1, T2, T3]) = + '{ (${tup._1.toExpr}, ${tup._2.toExpr}, ${tup._3.toExpr}) } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable] as Liftable[Tuple4[T1, T2, T3, T4]] = new { + def toExpr(tup: Tuple4[T1, T2, T3, T4]) = + '{ (${tup._1.toExpr}, ${tup._2.toExpr}, ${tup._3.toExpr}, ${tup._4.toExpr}) } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable] as Liftable[Tuple5[T1, T2, T3, T4, T5]] = new { + def toExpr(tup: Tuple5[T1, T2, T3, T4, T5]) = { + val (x1, x2, x3, x4, x5) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable] as Liftable[Tuple6[T1, T2, T3, T4, T5, T6]] = new { + def toExpr(tup: Tuple6[T1, T2, T3, T4, T5, T6]) = { + val (x1, x2, x3, x4, x5, x6) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable] as Liftable[Tuple7[T1, T2, T3, T4, T5, T6, T7]] = new { + def toExpr(tup: Tuple7[T1, T2, T3, T4, T5, T6, T7]) = { + val (x1, x2, x3, x4, x5, x6, x7) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable] as Liftable[Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]] = new { + def toExpr(tup: Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable] as Liftable[Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]] = new { + def toExpr(tup: Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable] as Liftable[Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]] = new { + def toExpr(tup: Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable] as Liftable[Tuple11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11]] = new { + def toExpr(tup: Tuple11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable] as Liftable[Tuple12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12]] = new { + def toExpr(tup: Tuple12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable] as Liftable[Tuple13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13]] = new { + def toExpr(tup: Tuple13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable] as Liftable[Tuple14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14]] = new { + def toExpr(tup: Tuple14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable, T15: Type: Liftable] as Liftable[Tuple15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15]] = new { + def toExpr(tup: Tuple15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}, ${x15.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable, T15: Type: Liftable, T16: Type: Liftable] as Liftable[Tuple16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16]] = new { + def toExpr(tup: Tuple16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}, ${x15.toExpr}, ${x16.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable, T15: Type: Liftable, T16: Type: Liftable, T17: Type: Liftable] as Liftable[Tuple17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17]] = new { + def toExpr(tup: Tuple17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}, ${x15.toExpr}, ${x16.toExpr}, ${x17.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable, T15: Type: Liftable, T16: Type: Liftable, T17: Type: Liftable, T18: Type: Liftable] as Liftable[Tuple18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18]] = new { + def toExpr(tup: Tuple18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}, ${x15.toExpr}, ${x16.toExpr}, ${x17.toExpr}, ${x18.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable, T15: Type: Liftable, T16: Type: Liftable, T17: Type: Liftable, T18: Type: Liftable, T19: Type: Liftable] as Liftable[Tuple19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19]] = new { + def toExpr(tup: Tuple19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}, ${x15.toExpr}, ${x16.toExpr}, ${x17.toExpr}, ${x18.toExpr}, ${x19.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable, T15: Type: Liftable, T16: Type: Liftable, T17: Type: Liftable, T18: Type: Liftable, T19: Type: Liftable, T20: Type: Liftable] as Liftable[Tuple20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20]] = new { + def toExpr(tup: Tuple20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}, ${x15.toExpr}, ${x16.toExpr}, ${x17.toExpr}, ${x18.toExpr}, ${x19.toExpr}, ${x20.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable, T15: Type: Liftable, T16: Type: Liftable, T17: Type: Liftable, T18: Type: Liftable, T19: Type: Liftable, T20: Type: Liftable, T21: Type: Liftable] as Liftable[Tuple21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21]] = new { + def toExpr(tup: Tuple21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}, ${x15.toExpr}, ${x16.toExpr}, ${x17.toExpr}, ${x18.toExpr}, ${x19.toExpr}, ${x20.toExpr}, ${x21.toExpr}) } + } + } + + given [T1: Type: Liftable, T2: Type: Liftable, T3: Type: Liftable, T4: Type: Liftable, T5: Type: Liftable, T6: Type: Liftable, T7: Type: Liftable, T8: Type: Liftable, T9: Type: Liftable, T10: Type: Liftable, T11: Type: Liftable, T12: Type: Liftable, T13: Type: Liftable, T14: Type: Liftable, T15: Type: Liftable, T16: Type: Liftable, T17: Type: Liftable, T18: Type: Liftable, T19: Type: Liftable, T20: Type: Liftable, T21: Type: Liftable, T22: Type: Liftable] as Liftable[Tuple22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22]] = new { + def toExpr(tup: Tuple22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22]) = { + val (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22) = tup + '{ (${x1.toExpr}, ${x2.toExpr}, ${x3.toExpr}, ${x4.toExpr}, ${x5.toExpr}, ${x6.toExpr}, ${x7.toExpr}, ${x8.toExpr}, ${x9.toExpr}, ${x10.toExpr}, ${x11.toExpr}, ${x12.toExpr}, ${x13.toExpr}, ${x14.toExpr}, ${x15.toExpr}, ${x16.toExpr}, ${x17.toExpr}, ${x18.toExpr}, ${x19.toExpr}, ${x20.toExpr}, ${x21.toExpr}, ${x22.toExpr}) } + } + } + } diff --git a/tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala b/tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala index 9e3718a26938..c6b51cc1e1ec 100644 --- a/tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala +++ b/tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala @@ -110,11 +110,4 @@ object TypeToolbox { companionClassOpt.map(_.fullName).getOrElse("").toExpr } - // TODO add to the std lib - private implicit def listIsLiftable[T: Type: Liftable]: Liftable[List[T]] = new Liftable { - def toExpr(list: List[T]) = list match { - case x :: xs => '{${x.toExpr} :: ${toExpr(xs)}} - case Nil => '{Nil} - } - } } diff --git a/tests/run-macros/quote-force/quoted_1.scala b/tests/run-macros/quote-force/quoted_1.scala index b537f3f847ca..7af3975e7183 100644 --- a/tests/run-macros/quote-force/quoted_1.scala +++ b/tests/run-macros/quote-force/quoted_1.scala @@ -12,10 +12,4 @@ object Location { '{new Location(${list})} } - private implicit def ListIsLiftable[T : Liftable : Type]: Liftable[List[T]] = new Liftable[List[T]] { - def toExpr(x: List[T]) = x match { - case x :: xs => '{ ${x} :: ${xs} } - case Nil => '{ List.empty[T] } - } - } } diff --git a/tests/run-macros/tasty-interpolation-1/Macro.scala b/tests/run-macros/tasty-interpolation-1/Macro.scala index 3f64a3e8a7e4..2e803cb7817d 100644 --- a/tests/run-macros/tasty-interpolation-1/Macro.scala +++ b/tests/run-macros/tasty-interpolation-1/Macro.scala @@ -77,16 +77,7 @@ abstract class MacroStringInterpolator[T] { } protected implicit def StringContextIsLiftable: Liftable[StringContext] = new Liftable[StringContext] { - def toExpr(strCtx: StringContext) = { - // TODO define in stdlib? - implicit def ListIsLiftable: Liftable[List[String]] = new Liftable[List[String]] { - override def toExpr(list: List[String]) = list match { - case x :: xs => '{${x.toExpr} :: ${toExpr(xs)}} - case Nil => '{Nil} - } - } - '{StringContext(${strCtx.parts.toList}: _*)} - } + def toExpr(strCtx: StringContext) = '{StringContext(${strCtx.parts.toList}: _*)} } protected class NotStaticlyKnownError(msg: String, expr: Expr[Any]) extends Exception(msg) diff --git a/tests/run-macros/tasty-location/quoted_1.scala b/tests/run-macros/tasty-location/quoted_1.scala index b350cf9c6fa4..15f0ffa15281 100644 --- a/tests/run-macros/tasty-location/quoted_1.scala +++ b/tests/run-macros/tasty-location/quoted_1.scala @@ -18,10 +18,4 @@ object Location { '{new Location(${list})} } - private implicit def ListIsLiftable[T : Liftable : Type]: Liftable[List[T]] = new Liftable[List[T]] { - def toExpr(x: List[T]) = x match { - case x :: xs => '{ $x :: $xs } - case Nil => '{ List.empty[T] } - } - } } diff --git a/tests/run-with-compiler/quote-lib.scala b/tests/run-with-compiler/quote-lib.scala index 04726cd8de57..94598f49d35f 100644 --- a/tests/run-with-compiler/quote-lib.scala +++ b/tests/run-with-compiler/quote-lib.scala @@ -5,7 +5,6 @@ import scala.quoted.autolift._ import liftable.Units._ import liftable.Lets._ import liftable.Loops._ -import liftable.Tuples._ import liftable.Lists._ import liftable.Exprs._ @@ -25,16 +24,60 @@ object Test { val t2: Expr[(Int, Int)] = (2, 3) val t3: Expr[(Int, Int, Int)] = (2, 3, 4) val t4: Expr[(Int, Int, Int, Int)] = (2, 3, 4, 5) + (1, 2, 3, 4).toExpr + (1, 2, 3, 4, 5).toExpr + (1, 2, 3, 4, 5, 6).toExpr + (1, 2, 3, 4, 5, 6, 7).toExpr + (1, 2, 3, 4, 5, 6, 7, 8).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21).toExpr + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22).toExpr +// (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23).toExpr +// (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24).toExpr +// (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25).toExpr val list: List[Int] = List(1, 2, 3) val liftedList: Expr[List[Int]] = list + val seq: Seq[Int] = Seq(1, 2, 3) + val liftedSeq: Expr[Seq[Int]] = seq + + val set: Set[Int] = Set(1, 2, 3) + val liftedSet: Expr[Set[Int]] = set + + val map: Map[Int, Char] = Map(1 -> 'a', 2 -> 'b', 3 -> 'c') + val liftedMap: Expr[Map[Int, Char]] = map + liftedList.foldLeft[Int](0)('{ (acc: Int, x: Int) => acc + x }).show liftedList.foreach('{ (x: Int) => println(x) }).show list.unrolledFoldLeft[Int](0)('{ (acc: Int, x: Int) => acc + x }).show list.unrolledForeach('{ (x: Int) => println(x) }).show + val iarray: IArray[Int] = IArray(1, 2, 3) + val liftedIArray: Expr[IArray[Int]] = iarray + + val some: Option[Int] = Some(2) + val none: Option[Int] = Some(2) + val liftedSome: Expr[Option[Int]] = some + val liftedNone: Expr[Option[Int]] = none + + val left: Either[Int, Long] = Left(1) + val right: Either[Int, Long] = Right(2L) + left.toExpr + right.toExpr + println("quote lib ok") } } @@ -71,42 +114,8 @@ package liftable { def liftedDoWhile(body: Expr[Unit])(cond: Expr[Boolean]) given QuoteContext: Expr[Unit] = '{ do $body while ($cond) } } - object Tuples { - - implicit def Tuple1IsLiftable[T1: Liftable](implicit t1: Type[T1]): Liftable[Tuple1[T1]] = new Liftable[Tuple1[T1]] { - def toExpr(x: Tuple1[T1]) = - '{ Tuple1[$t1](${ x._1}) } - } - - implicit def Tuple2IsLiftable[T1: Liftable, T2: Liftable](implicit t1: Type[T1], t2: Type[T2]): Liftable[(T1, T2)] = new Liftable[(T1, T2)] { - def toExpr(x: (T1, T2)) = - '{ Tuple2[$t1, $t2](${x._1}, ${x._2}) } - - } - - implicit def Tuple3IsLiftable[T1: Liftable, T2: Liftable, T3: Liftable](implicit t1: Type[T1], t2: Type[T2], t3: Type[T3]): Liftable[(T1, T2, T3)] = new Liftable[(T1, T2, T3)] { - def toExpr(x: (T1, T2, T3)) = - '{ Tuple3[$t1, $t2, $t3](${x._1}, ${x._2}, ${x._3}) } - - } - - implicit def Tuple4IsLiftable[T1: Liftable, T2: Liftable, T3: Liftable, T4: Liftable](implicit t1: Type[T1], t2: Type[T2], t3: Type[T3], t4: Type[T4]): Liftable[(T1, T2, T3, T4)] = new Liftable[(T1, T2, T3, T4)] { - def toExpr(x: (T1, T2, T3, T4)) = - '{ Tuple4[$t1, $t2, $t3, $t4](${x._1}, ${x._2}, ${x._3}, ${x._4}) } - } - - // TODO more tuples - - } - object Lists { - implicit def ListIsLiftable[T: Liftable](implicit t: Type[T]): Liftable[List[T]] = new Liftable[List[T]] { - def toExpr(x: List[T]): given QuoteContext => Expr[List[T]] = x match { - case x :: xs => '{ (${xs}).::[$t](${x}) } - case Nil => '{ Nil: List[$t] } - } - } implicit class LiftedOps[T: Liftable](list: Expr[List[T]])(implicit t: Type[T]) { def foldLeft[U](acc: Expr[U])(f: Expr[(U, T) => U])(implicit u: Type[U], qctx: QuoteContext): Expr[U] = @@ -126,11 +135,5 @@ package liftable { } } - object Arrays { - implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = new Liftable[Array[T]] { - def toExpr(arr: Array[T]) = '{ new Array[$t](${arr.length})($ct) } - } - } - } }