trait
Generic[T] extends Serializable
Type Members
-
abstract
type
Repr
Abstract Value Members
-
abstract
def
from(r: Repr): T
-
abstract
def
to(t: T): Repr
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: Any): Boolean
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
-
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
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:
Note that constituents of Cat and Dog are exactly the same - a String and an Int. So we could do:
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):
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. }}}
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.