shadedshapeless

Generic

trait Generic[T] extends Serializable

Represents the ability to convert from a concrete type (e.g. a case class) to a generic (HList / Coproduct} based) representation of the type.

For example:

scala> sealed trait Animal

defined trait Animal
scala> case class Cat(name: String, livesLeft: Int) extends Animal
defined class Cat

scala> case class Dog(name: String, bonesHidden: Int) extends Animal
defined class Dog

scala> val genCat = Generic[Cat]
genCat: shadedshapeless.Generic[Cat]{ type Repr = String :: Int :: HNil } = ...

scala> val genDog = Generic[Dog]
genDog: shadedshapeless.Generic[Dog]{ type Repr = String :: Int :: HNil } = ...

scala> val garfield = Cat("Garfield", 9)
garfield: Cat = Cat(Garfield,9)

scala> val genGarfield = genCat.to(garfield)
genGarfield: genCat.Repr = Garfield :: 9 :: HNil

scala> val reconstructed = genCat.from(genGarfield)
reconstructed: Cat = Cat(Garfield,9)

scala> reconstructed == garfield
res0: Boolean = true

Note that constituents of Cat and Dog are exactly the same - a String and an Int. So we could do:

scala> val odieAsCat = genCat.from(genDog.to(odie))
odieAsCat: Cat = Cat(odie,3)

This is quite useful in certain cases, such as copying from one object type to another, as in schema evolution.

Note that the generic representation depends on the type at which we instantiate Generic. In the example above we instantiated it at Cat and at Dog, and so the generic representation gave the minimal constituents of each of those.

However, if we instantiate Generic[Animal] instead the generic representation would encode the Cat-ness or Dog-ness of the instance as well (see Coproduct for details of the encoding):

scala> genDog.to(odie)
res9: genDog.Repr = odie :: 3 :: HNil

scala> val genAnimal = Generic[Animal]
genAnimal: shadedshapeless.Generic[Animal]{ type Repr = Cat :+: Dog :+: CNil } = ...

scala> genAnimal.to(odie)
res8: genAnimal.Repr = Dog(odie,3)

scala> genAnimal.to(odie) match { case Inr(Inl(dog)) => dog; case _ => null }
res9: Dog = Dog(odie,3)

Inr and Inl are shadedshapeless.Coproduct constructors. Shapeless constructs each class representation as a sort of "nested Either" using Coproduct. So in our example, genAnimal would essentially encode garfield as Inl(garfield) and odie as Inr(Inl(odie)). Please see shadedshapeless.Coproduct for more details. }}}

T

An immutable data type that has a canonical way of constructing and deconstructing instances (e.g. via apply / unapply). Sealed families of case classes work best.

Linear Supertypes
Serializable, Serializable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Generic
  2. Serializable
  3. Serializable
  4. AnyRef
  5. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. abstract type Repr

    The generic representation type for {T}, which will be composed of {Coproduct} and {HList} types

Abstract Value Members

  1. abstract def from(r: Repr): T

    Convert an instance of the generic representation to an instance of the concrete type

  2. abstract def to(t: T): Repr

    Convert an instance of the concrete type to the generic value representation

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  18. def toString(): String

    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped