Skip to content

Commit 31c0f08

Browse files
committed
WIP Add quoted.Liftable[IArray[T]] to the stdlib
1 parent 5e3ea88 commit 31c0f08

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

library/src/scala/quoted/Liftable.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package scala.quoted
22

3+
import scala.reflect.ClassTag
4+
35
/** A typeclass for types that can be turned to `quoted.Expr[T]`
46
* without going through an explicit `'{...}` operation.
57
*/
@@ -43,6 +45,13 @@ object Liftable {
4345
}
4446
}
4547

48+
given [T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] {
49+
def toExpr(iarray: IArray[T]): given QuoteContext => Expr[IArray[T]] = '{ // TODO specialize
50+
val array = new Array[T](${Liftable_Int_delegate.toExpr(iarray.length)})(ClassTag(${ClassIsLiftable.toExpr(the[ClassTag[T]].runtimeClass)}))
51+
${ Expr.block(List.tabulate(iarray.length)(i => '{ array(${Liftable_Int_delegate.toExpr(i)}) = ${the[Liftable[T]].toExpr(iarray(i))} }), '{ array.asInstanceOf[IArray[T]] }) }
52+
}
53+
}
54+
4655
given [T: Type: Liftable] as Liftable[List[T]] = new Liftable[List[T]] {
4756
def toExpr(x: List[T]): given QuoteContext => Expr[List[T]] = x match {
4857
case x :: xs => '{ (${this.toExpr(xs)}).::[T](${the[Liftable[T]].toExpr(x)}) }

tests/run-with-compiler/quote-lib.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ object Test {
3535
list.unrolledFoldLeft[Int](0)('{ (acc: Int, x: Int) => acc + x }).show
3636
list.unrolledForeach('{ (x: Int) => println(x) }).show
3737

38+
val iarray: IArray[Int] = IArray(1, 2, 3)
39+
val liftedIArray: Expr[IArray[Int]] = iarray
40+
3841
println("quote lib ok")
3942
}
4043
}
@@ -120,11 +123,5 @@ package liftable {
120123
}
121124
}
122125

123-
object Arrays {
124-
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = new Liftable[Array[T]] {
125-
def toExpr(arr: Array[T]) = '{ new Array[$t](${arr.length})($ct) }
126-
}
127-
}
128-
129126
}
130127
}

0 commit comments

Comments
 (0)