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

Scala example source code file (FreshNames.scala)

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

freshnamecreator, freshnameextractor, freshnames, name, names, none, option, reflection, stdnames, string, termname, typename, utilities

The FreshNames.scala Scala example source code

/* NSC -- new Scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 */

package scala
package reflect
package internal

import scala.reflect.internal.util.FreshNameCreator

trait FreshNames { self: Names with StdNames =>
  // SI-6879 Keeps track of counters that are supposed to be globally unique
  //         as opposed to traditional freshers that are unique to compilation units.
  val globalFreshNameCreator = new FreshNameCreator

  // default fresh name creator used to abstract over currentUnit.fresh and runtime fresh name creator
  def currentFreshNameCreator: FreshNameCreator

  // create fresh term/type name using implicit fresh name creator
  def freshTermName(prefix: String = nme.FRESH_TERM_NAME_PREFIX)(implicit creator: FreshNameCreator): TermName = newTermName(creator.newName(prefix))
  def freshTypeName(prefix: String)(implicit creator: FreshNameCreator): TypeName = newTypeName(creator.newName(prefix))

  // Extractor that matches names which were generated by some
  // FreshNameCreator with known prefix. Extracts user-specified
  // prefix that was used as a parameter to newName by stripping
  // global creator prefix and unique number in the end of the name.
  class FreshNameExtractor(creatorPrefix: String = "") {
    // quote prefix so that it can be used with replaceFirst
    // which expects regExp rather than simple string
    val quotedCreatorPrefix = java.util.regex.Pattern.quote(creatorPrefix)

    def unapply(name: Name): Option[String] = {
      val sname = name.toString
      // name should start with creatorPrefix and end with number
      if (!sname.startsWith(creatorPrefix) || !sname.matches("^.*\\d*$")) None
      else Some(NameTransformer.decode(sname.replaceFirst(quotedCreatorPrefix, "").replaceAll("\\d*$", "")))
    }
  }
}

Other Scala source code examples

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.