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

Scala example source code file (CapturedVariables.scala)

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

boolean, captured, notype, objectrefclass, referencetoboxed, symbol, symboltable, tree, type, volatileobjectrefclass

The CapturedVariables.scala Scala example source code

package scala
package reflect
package internal

import Flags._

trait CapturedVariables { self: SymbolTable =>

  import definitions._

  /** Mark a variable as captured; i.e. force boxing in a *Ref type.
   */
  def captureVariable(vble: Symbol): Unit = vble setFlag CAPTURED

  /** Mark given identifier as a reference to a captured variable itself
   *  suppressing dereferencing with the `elem` field.
   */
  def referenceCapturedVariable(vble: Symbol): Tree = ReferenceToBoxed(Ident(vble))

  /** Convert type of a captured variable to *Ref type.
   */
  def capturedVariableType(vble: Symbol): Type =
    capturedVariableType(vble, NoType, erasedTypes = false)

  /** Convert type of a captured variable to *Ref type.
   */
  def capturedVariableType(vble: Symbol, tpe: Type = NoType, erasedTypes: Boolean = false): Type = {
    val tpe1 = if (tpe == NoType) vble.tpe else tpe
    val symClass = tpe1.typeSymbol
    def refType(valueRef: Map[Symbol, Symbol], objectRefClass: Symbol) =
      if (isPrimitiveValueClass(symClass) && symClass != UnitClass) valueRef(symClass).tpe
      else if (erasedTypes) objectRefClass.tpe
      else appliedType(objectRefClass, tpe1)
    if (vble.hasAnnotation(VolatileAttr)) refType(volatileRefClass, VolatileObjectRefClass)
    else refType(refClass, ObjectRefClass)
  }
}

Other Scala source code examples

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