alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Liftables.scala)

This example Scala source code file (Liftables.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

liftable, liftables, option, partialfunction, standardliftableinstances, standardunliftableinstances, t, tree, universe, unliftable

The Liftables.scala Scala example source code

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 corresponing 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)
    }
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala Liftables.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.