summaryrefslogtreecommitdiff
path: root/src/reflect/scala/reflect/api/Liftables.scala
blob: c6352905d1c6dee9f50213079c583cfbd9c5edff (plain) (blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package scala
package reflect
package api

trait Liftables { self: Universe =>

  /** A type class that defines a representation of `T` as a `Tree`.
   *
   *  @see [[http://docs.scala-lang.org/overviews/quasiquotes/lifting.html]]
   */
  trait Liftable[T] {
    def apply(value: T): Tree
  }

  /** Companion to `Liftable` type class that contains standard instances
   *  and provides a helper `apply` method to simplify creation of new ones.
   */
  object Liftable extends StandardLiftableInstances {
    /** A helper method that simplifies creation of `Liftable` instances.
     *  Takes a type and a function that maps that type to a tree representation.
     *
     *  For example to write Liftable for object one might use it like:
     *
     *  {{{
     *  scala> object O
     *
     *  scala> val Oref = symbolOf[O.type].asClass.module
     *
     *  scala> implicit val liftO = Liftable[O.type] { _ => q"$Oref" }
     *
     *  scala> val lifted = q"$O"
     *  lifted: universe.Tree = O
     *  }}}
     *
     *  @see [[http://docs.scala-lang.org/overviews/quasiquotes/lifting.html]]
     */
    def apply[T](f: T => Tree): Liftable[T] =
      new Liftable[T] { def apply(value: T): Tree = f(value) }
  }

  /** A type class that defines a way to extract instance of `T` from a `Tree`.
   *
   *  @see [[http://docs.scala-lang.org/overviews/quasiquotes/unlifting.html]]
   */
  trait Unliftable[T] {
    def unapply(tree: Tree): Option[T]
  }

  /** Companion to `Unliftable` type class that contains standard instances
   *  and provides a helper `apply` method to simplify creation of new ones.
   */
  object Unliftable extends StandardUnliftableInstances {
    /** A helper method that simplifies creation of `Unliftable` instances.
     *  Takes a partial function which is defined on correct representations of `T`
     *  and returns corresponding instances.
     *
     *  For example to extract a reference to an object as object itself:
     *
     *  {{{
     *  scala> object O
     *
     *  scala> val Oref = symbolOf[O.type].asClass.module
     *
     *  scala> implicit val unliftO = Unliftable[O.type] { case t if t.symbol == Oref => O }
     *
     *  scala> val q"${_: O.type}" = q"$Oref"
     *  }}}
     *
     *  @see [[http://docs.scala-lang.org/overviews/quasiquotes/unlifting.html]]
     */
    def apply[T](pf: PartialFunction[Tree, T]): Unliftable[T] = new Unliftable[T] {
      def unapply(value: Tree): Option[T] = pf.lift(value)
    }
  }
}