Package

s_mach

codetools

Permalink

package codetools

Linear Supertypes
CodeToolsImplicits, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. codetools
  2. CodeToolsImplicits
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. trait CodeToolsImplicits extends AnyRef

    Permalink
  2. trait IsDistinctTypeAlias[A] extends AnyRef

    Permalink

    Marker trait used to mark a type alias as being a distinct type alias (DTA).

    Marker trait used to mark a type alias as being a distinct type alias (DTA). A DTA is an alternative to the Scala value-class (http://docs.scala-lang.org/overviews/core/value-classes.html) that never needs to box (or unbox) since type aliases are eliminated in byte code.

    A DTA is always declared with a unique trait tag to make the type alias distinct in the type system. Since the DTA is a distinct type, type-classes of the DTA can be implicitly resolved. In Scala, a normal type alias would always resolve to its underlying type.

    Tagging a type alias example: type Age1 = Int // implicitly[Format[Age] ] resolves to Format[Int] trait AgeTag type Age = Int with AgeTag // implicitly[Format[Age] ] is distinct from Format[Int]

    Value-classes are elided by the compiler under many circumstances, but when used in any kind of generic, such as List or Option, they are emitted. To convert from a generic of the value-class type to the underlying type requires a no-op runtime call in byte code that may or may not be eliminated by downstream optimization. For collections such as List, this means a O(n) call to map simply to cast from the value-class to the underlying type. Because DTAs are simply type aliases for their underlying type the compiler can simply treat a generic of a DTA as a generic of the underlying type without any impact on runtime.

    Converting the other direction for value-classes suffers from the same problem as stated above. Because DTAs require a distinct tag, the compiler can not automatically perform the downcast. Instead, implicit defs are created to support the operation as a casting operation that should be eliminated in otput byte code.

    Example generic converter: implicit def ma_to_mv[M[_],V < : IsDistinctTypeAlias[A],A]( ma: M[A] ) : M[V] = ma.asInstanceOf[M[V] ]

    A DTA is declared by first creating a trait tag that is used to make the DTA distinct. Next, the type alias is declared as a mix of the underlying type, the tag and the marker trait IsDistinctTypeAlias. The IsDistinctTypeAlias marker trait is used to in implicit def conversions that convert generics (such as collections) without overhead.

    Full example: trait AgeTag type Age = Int with AgeTag with IsDistinctTypeAlias[Int] implicit def Age(i: Int) = i.asInstanceOf[Age]

  3. trait IsValueClass[A] extends Any

    Permalink

    A base trait for a user-defined value-class that standardizes the name of the value-class val to "underlying" and toString to underlying toString.

    A base trait for a user-defined value-class that standardizes the name of the value-class val to "underlying" and toString to underlying toString. This allows creating an implicit conversion from an instance of any value-class to its underlying representation.

    For details on value-class see: http://docs.scala-lang.org/overviews/core/value-classes.html

    Example value-class: implicit class Name(underlying: String) extends AnyVal with IsValueType[String]

    Note1: When using creating a value-class for String, it is necessary to create an implicit view to StringOps to use the Scala extended String methods on instances of the value-class. Example for Name above: object Name { import scala.collection.immutable.StringOps implicit def stringOps_Name(n: Name) = new StringOps(n.underlying) }

    Note2: while almost every method on underlying can be used on the value-class without special code, the equals method is a special case that still requires wrapping the right-hand type in the value-class to match the other side. Ex:

    Name("Hal") == "Hal" // always returns false + compiler warning Name("Hal") == Name("Hal") // works correctly

    A

    type of underlying value class (Note: this parameter does not require inheritance from AnyVal since this would prevent using the trait with java.lang.String which does not inherit AnyVal)

Value Members

  1. object CodeToolsImplicits extends CodeToolsImplicits

    Permalink
  2. implicit def distinctTypeAlias_MAtoMV[M[_], V <: IsDistinctTypeAlias[A], A](ma: M[V]): M[A]

    Permalink

    Implicitly convert any generic M[V] to M[A] where V is a distinct type alias of A

    Implicitly convert any generic M[V] to M[A] where V is a distinct type alias of A

    M

    generic type

    V

    DTA type

    A

    underlying type of DTA

    ma

    generic instance

    returns

    instance cast to M[V]

    Definition Classes
    CodeToolsImplicits
    Annotations
    @inline()
  3. implicit def distinctTypeAlias_MVtoMA[M[_], V <: IsDistinctTypeAlias[A], A](ma: M[A]): M[V]

    Permalink

    Implicitly convert any generic M[A] to M[V] where V is a distinct type alias of A

    Implicitly convert any generic M[A] to M[V] where V is a distinct type alias of A

    M

    generic type

    V

    DTA type

    A

    underlying type of DTA

    ma

    generic instance

    returns

    instance cast to M[V]

    Definition Classes
    CodeToolsImplicits
    Annotations
    @inline()
  4. implicit def distinctTypeAlias_m2lVtoM2la[M2L[_, _], A, V <: IsDistinctTypeAlias[A], B](ma: M2L[A, B]): M2L[V, B]

    Permalink

    Implicitly convert any generic M[A,B] to a M[V,B] where V is a distinct type alias of A

    Implicitly convert any generic M[A,B] to a M[V,B] where V is a distinct type alias of A

    M2L

    type of generic instance

    A

    underlying type of DTA

    V

    DTA type

    B

    some type

    ma

    generic instance

    returns

    instance cast to M[V,B]

    Definition Classes
    CodeToolsImplicits
    Annotations
    @inline()
  5. implicit def distinctTypeAlias_m2rVtoM2ra[M2R[_, _], A, V <: IsDistinctTypeAlias[A], B](ma: M2R[B, A]): M2R[B, V]

    Permalink

    Implicitly convert any generic M[A,B] to a M[A,V] where V is a distinct type alias of B

    Implicitly convert any generic M[A,B] to a M[A,V] where V is a distinct type alias of B

    M2R

    type of generic instance

    A

    some type

    V

    DTA type

    B

    underlying type of DTA

    ma

    generic instance

    returns

    instance cast to M[A,V]

    Definition Classes
    CodeToolsImplicits
    Annotations
    @inline()
  6. package impl

    Permalink
  7. package reflectPrint

    Permalink
  8. implicit def valueClassToA[A](v: IsValueClass[A]): A

    Permalink

    Implicitly convert a value class to its underlying type

    Implicitly convert a value class to its underlying type

    A

    the underlying type

    v

    a value-class derived from IsValueClass

    returns

    the wrapped value

    Definition Classes
    CodeToolsImplicits
    Annotations
    @inline()

Inherited from CodeToolsImplicits

Inherited from AnyRef

Inherited from Any

Ungrouped