NewtypeK
For building newtypes over types that have an invariant type parameter (higher-kinded types).
Example:
// Only needed for type-class derivation
import cats._
import cats.implicits._
type Nel[A] = Nel.Type[A]
object Nel extends NewtypeK[List] {
def apply[A](head: A, tail: A*): Nel[A] =
unsafeCoerce(head :: tail.toList)
def unapply[F[_], A](list: F[A])(
implicit ev: F[A] =:= Nel[A]
): Some[(A, List[A])] = {
val l = value(list)
Some((l.head, l.tail))
}
implicit def eq[A: Eq]: Eq[Nel[A]] =
derive
implicit val traverse: Traverse[Nel] =
deriveK
implicit val monad: Monad[Nel] =
deriveK
}
NOTE: the type-parameter is invariant. See NewtypeCovariantK for working with covariant type parameters.
trait NewtypeTraitK[Src]
trait NewEncodingK[Src]
class Object
trait Matchable
class Any