Package

emblem

Permalink

package emblem

a collection of utilities for reflecting on types

Source
package.scala
Linear Supertypes
Content Hierarchy Learn more about scaladoc diagrams
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. emblem
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Type Members

  1. case class Emblem[T <: HasEmblem] extends Product with Serializable

    Permalink

    a reflective signature for a type.

    a reflective signature for a type. provides name information, properties, and a tool used to build new instances. the underlying type is treated as immutable, so each property provides a setter that returns a new instance. new instances can be built using a HasEmblemBuilder returned by method builder.

    T

    the type that this emblem reflects upon

  2. type EmblemPool = TypeKeyMap[HasEmblem, Emblem]

    Permalink

    a TypeKeyMap of HasEmblem to Emblem

  3. case class EmblemProp[A <: HasEmblem, B] extends EmblemPropPath[A, B] with Product with Serializable

    Permalink

    an emblem property.

    an emblem property. the property belongs to an Emblem, has a name, and a getter and a setter. because the emblem is treated as an immutable object, the setter returns a new instance.

    A

    the type that the containing emblem reflects upon

    B

    the property value type

  4. trait EmblemPropPath[A <: HasEmblem, B] extends AnyRef

    Permalink

    a property path that recurses through an emblem tree to a specific leaf

  5. case class Extractor[Domain, Range] extends Product with Serializable

    Permalink

    describes an injective relation between two types, Domain and Range.

    describes an injective relation between two types, Domain and Range. every value in the domain maps to a unique value in the range. however, not all values in the range map back onto the domain. the Range type is typically a richer type, such as a case class with a single parameter, and the Domain type is the type wrapped by the case class.

    provides functions for mapping between the range and domain types, as well as type keys for the two types. for example:

    case class Uri(uri: String)
    val extractor = Extractor[Uri, String]
    extractor.domainTypeKey should equal (typeKey[Uri])
    extractor.rangeTypeKey should equal (typeKey[String])
    extractor.apply(Uri("someUri")) should equal ("someUri")
    extractor.unapply("someUri") should equal (Some(Uri("someUri")))
    Domain

    the domain type

    Range

    the range type

  6. type ExtractorFor[Domain] = Extractor[Domain, _]

    Permalink

    an extractor with the domain type unspecified.

    an extractor with the domain type unspecified. this type is equivalent to Extractor[Domain, _], except with a single type parameter Domain. this allows the extractor to be used as a key or value in a TypeBoundMap or TypeKeyMap

    See also

    ExtractorPool

  7. type ExtractorPool = TypeKeyMap[Any, ExtractorFor]

    Permalink

    A TypeKeyMap of Domain to Extractor

  8. type Function0[A] = () ⇒ A

    Permalink

    a no-arg function with return type A

  9. trait HasEmblem extends AnyRef

    Permalink

    a marker trait for a type that we want to reflect upon with an Emblem

  10. class HasEmblemBuilder[T <: HasEmblem] extends AnyRef

    Permalink

    a builder of objects that have an emblem.

    a builder of objects that have an emblem.

    T

    the type of the object to build

  11. trait TypeBoundFunction[TypeBound, Arg[_ <: TypeBound], ReturnVal[_ <: TypeBound]] extends WideningTypeBoundFunction[TypeBound, TypeBound, Arg, ReturnVal]

    Permalink

    a function with one type parameter, where both the argument and the return value are types with a single type parameter, bound to the type parameter of the function.

    a function with one type parameter, where both the argument and the return value are types with a single type parameter, bound to the type parameter of the function.

    TypeBound

    the type bound to use for the argument and return value types

    Arg

    the argument type

    ReturnVal

    the return value type

  12. class TypeBoundMap[TypeBound, Key[_ <: TypeBound], Val[_ <: TypeBound]] extends BaseTypeBoundMap[TypeBound, Key, Val]

    Permalink

    a map where the types for keys and values share a type parameter with the same bounds.

    a map where the types for keys and values share a type parameter with the same bounds. the key and value of each key/value pair are constrained to match on that type parameter. for example, we might have some pet stores that only cater to a single kind of pet:

    trait Pet
    case class Cat(name: String) extends Pet
    case class Dog(name: String) extends Pet
    class PetStore[P <: Pet]
    
    val catStore1 = new PetStore[Cat]
    val catStore2 = new PetStore[Cat]
    val dogStore1 = new PetStore[Dog]

    we can use a TypeBoundMap to store a list of pets of the appropriate type for every pet store:

    var inventories = TypeBoundMap[Pet, PetStore, List]
    inventories += (catStore1 -> List(Cat("cat11"), Cat("cat12"), Cat("cat13")))
    inventories += (catStore2 -> List(Cat("cat21")))
    inventories += (dogStore1 -> List(Dog("dog11"), Dog("dog12")))

    now we can look up pet lists by pet store, with everything coming back as the expected type:

    val cats1: List[Cat] = inventories(catStore1)
    cats1.size should be (3)
    val cats2: List[Cat] = inventories(catStore2)
    cats2.size should be (1)
    val dogs1: List[Dog] = inventories(dogStore1)
    dogs1.size should be (2)
    
    val cat: Cat = inventories(catStore1).head
    cat should equal (Cat("cat11"))
    val dog: Dog = inventories(dogStore1).head
    dog should equal (Dog("dog11"))

    note that the API does not provide ++ or similar methods to add multiple key/value pairs at a time, as each pair needs to be type-checked separately.

    (the code presented here is in TypeBoundMapSpec.scala, up at the top)

    TypeBound

    the upper bound on the type parameters passed to the Key and Val types

    Key

    the parameterized type of the keys in the map

    Val

    the parameterized type of the values in the map

    See also

    TypeBoundMapSpec.scala and BaseTypeBoundMapSpec.scala for many more examples

  13. case class TypeBoundPair[TypeBound, A[_ <: TypeBound], B[_ <: TypeBound], TypeParam <: TypeBound](_1: A[TypeParam], _2: B[TypeParam]) extends Product with Serializable

    Permalink

    mimics a pair found in an ordinary map, but preserves the type parameter equality in the two elements of the pair

    mimics a pair found in an ordinary map, but preserves the type parameter equality in the two elements of the pair

    TypeBound

    the upper bound on the type parameters passed into the A and B types of the two elements of this pair

    A

    the parameterized type of the first element of this pair

    B

    the parameterized type of the second element of this pair

    TypeParam

    the type param binding both the A and B types of the two elements of this pair

    _1

    the first element of this type bound pair

    _2

    the second element of this type bound pair

  14. case class TypeKey[A](tag: scala.reflect.api.JavaUniverse.TypeTag[A]) extends Product with Serializable

    Permalink

    behaves much like a scala.reflect.runtime.universe.TypeTag, except that it can also be safely used as a key in a hash or a set.

    behaves much like a scala.reflect.runtime.universe.TypeTag, except that it can also be safely used as a key in a hash or a set. Two type keys will be equal if and only if their underlying types are equivalent according to method =:= in scala.reflect.api.Types.Type. The hashCode method does its best to produce unique hash values, and always produces values compatible with equals.

    type keys are provided by an implicit method in package emblem, so you can get one implicitly like so:

    def foo[A : TypeKey]() = {
      val key = implicitly[TypeKey[A]]
    }

    or you can get one explicitly like so:

    val key = emblem.typeKey[List[String]]

    or if you already have a TypeTag at hand:

    val tag: TypeTag[A] = ???
    val key = TypeKey(tag)
    A

    the type that we are keying one

    tag

    the scala-reflect TypeTag for type A

  15. class TypeKeyMap[TypeBound, Val[_ <: TypeBound]] extends BaseTypeBoundMap[TypeBound, TypeKey, Val]

    Permalink

    a map where the keys are type keys with an upper bound, and the values have a type parameter with the same bound.

    a map where the keys are type keys with an upper bound, and the values have a type parameter with the same bound. The key and value of each key/value pair are constrained to match on that type parameter. For example, suppose we are maintaining an inventory of computer parts:

    sealed trait ComputerPart
    case class Memory(gb: Int) extends ComputerPart
    case class CPU(mhz: Double) extends ComputerPart
    case class Display(resolution: Int) extends ComputerPart

    we can use a TypeKeyMap to store a list of parts for each kind of part:

    var partLists = TypeKeyMap[ComputerPart, List]()
    partLists += Memory(2) :: Memory(4) :: Memory(8) :: Nil
    partLists += CPU(2.2) :: CPU(2.4) :: CPU(2.6) :: Nil
    partLists += Display(720) :: Display(1080) :: Nil

    now we can look up part lists by part type, with everything coming back as the expected type:

    val memories: List[Memory] = partLists[Memory]
    memories.size should be (3)
    val cpus: List[CPU] = partLists[CPU]
    cpus.size should be (3)
    val displays: List[Display] = partLists[Display]
    displays.size should be (2)
    
    val cpu: CPU = partLists[CPU].head
    cpu should equal (CPU(2.2))
    val display: Display = partLists[Display].tail.head
    display should equal (Display(1080))

    note that the API does not provide ++ or similar methods to add multiple key/value pairs at a time, as each pair needs to be type-checked separately.

    (code presented here is in TypeKeyMapSpec.scala, up at the top)

    TypeBound

    the upper bound on the type parameters passed to the TypeKey and Val types

    Val

    the parameterized type of the values in the map

    See also

    TypeKeyMapSpec.scala and BaseTypeBoundMapSpec.scala for many more examples

    ExtractorPool for an example of how to use type key maps when the value type is more sophisticated than just type with a single type parameter.

  16. trait WideningTypeBoundFunction[TypeBound, WiderTypeBound >: TypeBound, Arg[_ <: TypeBound], ReturnVal[_ <: WiderTypeBound]] extends AnyRef

    Permalink

    like a TypeBoundFunction, except that the type bound for the return value is wider than the type bound for the argument.

    like a TypeBoundFunction, except that the type bound for the return value is wider than the type bound for the argument. This is useful for mapWiden and mapValuesWiden methods in TypeKeyMap and TypeBoundMap that return a map with a wider type bound than the original.

    TypeBound

    the type bound to use for the argument type

    WiderTypeBound

    the type bound to use for the return value type

    Arg

    the argument type

    ReturnVal

    the return value type

    See also

    TypeBoundFunction

Value Members

  1. object Emblem extends Serializable

    Permalink
  2. object EmblemPool

    Permalink
  3. object EmblemPropPath

    Permalink

    an EmblemPropPath factory

  4. object Extractor extends Serializable

    Permalink
  5. object ExtractorPool

    Permalink
  6. object TypeBoundMap

    Permalink
  7. object TypeKeyMap

    Permalink
  8. object basicTypes

    Permalink

    the basic types are the leaf-level types that emblem knows how to process.

    the basic types are the leaf-level types that emblem knows how to process. currently, the following basic type are supported:

    • Boolean
    • Char
    • org.joda.time.DateTime
    • Double
    • Float
    • Int
    • Long
    • String
  9. package exceptions

    Permalink
  10. object imports

    Permalink

    a standard set of imports for emblem.

    a standard set of imports for emblem. this will bring in all you need for basic emblem usage, and won't pollute your namespace the way that import emblem._ will

  11. object jsonUtil

    Permalink

    general purpose functions for working with JSON

  12. object stringUtil

    Permalink

    generally useful utility functions for working with strings

  13. package traversors

    Permalink
  14. def typeKey[A](implicit arg0: TypeKey[A]): TypeKey[A]

    Permalink

    returns a TypeKey for the specified type A.

    returns a TypeKey for the specified type A. this method will only work where a TypeTag is implicitly available.

  15. implicit def typeKeyFromTag[A](implicit arg0: scala.reflect.api.JavaUniverse.TypeTag[A]): TypeKey[A]

    Permalink

    an implicit method for producing a TypeKey.

    an implicit method for producing a TypeKey. this method allows type keys to be available implicitly anywhere that the corresponding TypeTag is implicitly available.

Inherited from AnyRef

Inherited from Any

Ungrouped