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: shapeless.Generic[Cat]{ type Repr = String :: Int :: HNil } = ... scala> val genDog = Generic[Dog] genDog: shapeless.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: shapeless.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 shapeless.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 shapeless.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.
- Alphabetic
- By Inheritance
- Generic
- Serializable
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
abstract
type
Repr
The generic representation type for {T}, which will be composed of {Coproduct} and {HList} types
Abstract Value Members
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )