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

Scala example source code file (StdTags.scala)

This example Scala source code file (StdTags.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

apiuniverse, classtag, mirror, reflection, singleton, stdcontexttags, stdruntimetags, stdtags, type, typecreator, u

The StdTags.scala Scala example source code

package scala.tools
package reflect

import scala.reflect.{ClassTag, classTag}
import scala.reflect.api.{Mirror, TypeCreator, Universe => ApiUniverse}

// [Eugene++] Before 2.10 is released, I suggest we don't rely on automated type tag generation
// sure, it's convenient, but then refactoring reflection / reification becomes a pain
// `ClassTag` tags are fine, because they don't need a reifier to be generated

trait StdTags {
  val u: ApiUniverse with Singleton
  val m: Mirror[u.type]

  lazy val tagOfListOfString: u.TypeTag[List[String]] =
    u.TypeTag[List[String]](
      m,
      new TypeCreator {
        def apply[U <: ApiUniverse with Singleton](m: Mirror[U]): U # Type = {
          val u = m.universe
          u.appliedType(u.definitions.ListClass.toType, List(u.definitions.StringClass.toType))
        }
      })

  protected def tagOfStaticClass[T: ClassTag]: u.TypeTag[T] =
    u.TypeTag[T](
      m,
      new TypeCreator {
        def apply[U <: ApiUniverse with Singleton](m: Mirror[U]): U # Type =
          m.staticClass(classTag[T].runtimeClass.getName).toTypeConstructor.asInstanceOf[U # Type]
      })
  lazy val tagOfInt = u.TypeTag.Int
  lazy val tagOfString = tagOfStaticClass[String]
  lazy val tagOfFile = tagOfStaticClass[scala.tools.nsc.io.File]
  lazy val tagOfDirectory = tagOfStaticClass[scala.tools.nsc.io.Directory]
  lazy val tagOfThrowable = tagOfStaticClass[java.lang.Throwable]
  lazy val tagOfClassLoader = tagOfStaticClass[java.lang.ClassLoader]
  lazy val tagOfBigInt = tagOfStaticClass[BigInt]
  lazy val tagOfBigDecimal = tagOfStaticClass[BigDecimal]
  lazy val tagOfCalendar = tagOfStaticClass[java.util.Calendar]
  lazy val tagOfDate = tagOfStaticClass[java.util.Date]
}

object StdRuntimeTags extends StdTags {
  val u: scala.reflect.runtime.universe.type = scala.reflect.runtime.universe
  val m = u.runtimeMirror(getClass.getClassLoader)
  // we need getClass.getClassLoader to support the stuff from scala-compiler.jar
}

abstract class StdContextTags extends StdTags {
  val tc: scala.reflect.macros.contexts.Context
  val u: tc.universe.type = tc.universe
  val m = tc.mirror
}

Other Scala source code examples

Here is a short list of links related to this Scala StdTags.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.