Skip to content

Commit

Permalink
Give better names to UnliftHelper1 and UnliftHelper2
Browse files Browse the repository at this point in the history
Previous ones were inscrutable but thankfully @xeno_by
helped me out to find better alternatives.;
  • Loading branch information
densh committed Jan 16, 2014
1 parent ae4a2f0 commit 6283c01
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/compiler/scala/tools/reflect/quasiquotes/Holes.scala
Expand Up @@ -166,16 +166,15 @@ trait Holes { self: Quasiquotes =>
/** Full support for unliftable implies that it's possible to interleave
* deconstruction with higher cardinality and unlifting of the values.
* In particular extraction of List[Tree] as List[T: Unliftable] requires
* helper extractors that would do the job: UnliftHelper1[T]. Similarly
* List[List[Tree]] needs UnliftHelper2[T].
* helper extractors that would do the job: UnliftListElementwise[T]. Similarly
* List[List[Tree]] needs UnliftListOfListsElementwise[T].
*
* See also "unlift list" tests in UnapplyProps.scala
*/
object unlifters {
private var records = List.empty[(Type, Cardinality)]
// Request an UnliftHelperN[T] where n == card and T == tpe.
// If card == 0 then helper is not needed and plain instance
// of unliftable is returned.
// Materialize unlift helper that does elementwise
// unlifting for corresponding cardinality and type.
def spawn(tpe: Type, card: Cardinality): Option[Tree] = {
val unlifter = inferUnliftable(tpe)
if (unlifter == EmptyTree) None
Expand All @@ -190,7 +189,10 @@ trait Holes { self: Quasiquotes =>
def preamble(): List[Tree] =
records.zipWithIndex.map { case ((tpe, card), idx) =>
val name = TermName(nme.QUASIQUOTE_UNLIFT_HELPER + idx)
val helperName = card match { case DotDot => nme.UnliftHelper1 case DotDotDot => nme.UnliftHelper2 }
val helperName = card match {
case DotDot => nme.UnliftListElementwise
case DotDotDot => nme.UnliftListOfListsElementwise
}
val lifter = inferUnliftable(tpe)
assert(helperName.isTermName)
// q"val $name: $u.build.${helperName.toTypeName} = $u.build.$helperName($lifter)"
Expand Down
8 changes: 4 additions & 4 deletions src/reflect/scala/reflect/api/BuildUtils.scala
Expand Up @@ -246,13 +246,13 @@ private[reflect] trait BuildUtils { self: Universe =>
def unapply(tree: Tree): Option[(List[Tree], Tree)]
}

def UnliftHelper1[T](unliftable: Unliftable[T]): UnliftHelper1[T]
trait UnliftHelper1[T] {
def UnliftListElementwise[T](unliftable: Unliftable[T]): UnliftListElementwise[T]
trait UnliftListElementwise[T] {
def unapply(lst: List[Tree]): Option[List[T]]
}

def UnliftHelper2[T](unliftable: Unliftable[T]): UnliftHelper2[T]
trait UnliftHelper2[T] {
def UnliftListOfListsElementwise[T](unliftable: Unliftable[T]): UnliftListOfListsElementwise[T]
trait UnliftListOfListsElementwise[T] {
def unapply(lst: List[List[Tree]]): Option[List[List[T]]]
}

Expand Down
4 changes: 2 additions & 2 deletions src/reflect/scala/reflect/internal/BuildUtils.scala
Expand Up @@ -455,14 +455,14 @@ trait BuildUtils { self: SymbolTable =>
}
}

def UnliftHelper1[T](unliftable: Unliftable[T]) = new UnliftHelper1[T] {
def UnliftListElementwise[T](unliftable: Unliftable[T]) = new UnliftListElementwise[T] {
def unapply(lst: List[Tree]): Option[List[T]] = {
val unlifted = lst.flatMap { unliftable.unapply(_) }
if (unlifted.length == lst.length) Some(unlifted) else None
}
}

def UnliftHelper2[T](unliftable: Unliftable[T]) = new UnliftHelper2[T] {
def UnliftListOfListsElementwise[T](unliftable: Unliftable[T]) = new UnliftListOfListsElementwise[T] {
def unapply(lst: List[List[Tree]]): Option[List[List[T]]] = {
val unlifted = lst.map { l => l.flatMap { unliftable.unapply(_) } }
if (unlifted.flatten.length == lst.flatten.length) Some(unlifted) else None
Expand Down
4 changes: 2 additions & 2 deletions src/reflect/scala/reflect/internal/StdNames.scala
Expand Up @@ -763,8 +763,8 @@ trait StdNames {
val unapplySeq: NameType = "unapplySeq"
val unbox: NameType = "unbox"
val universe: NameType = "universe"
val UnliftHelper1: NameType = "UnliftHelper1"
val UnliftHelper2: NameType = "UnliftHelper2"
val UnliftListElementwise: NameType = "UnliftListElementwise"
val UnliftListOfListsElementwise: NameType = "UnliftListOfListsElementwise"
val update: NameType = "update"
val updateDynamic: NameType = "updateDynamic"
val value: NameType = "value"
Expand Down

0 comments on commit 6283c01

Please sign in to comment.