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> sealedtrait Animal
defined trait Animal
scala> caseclass Cat(name: String, livesLeft: Int) extends Animal
defined class Cat
scala> caseclass 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.
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.