我建议如下参数化类型:
case class Person[F[_]](name: F[String], age: F[Int])
然后您可以派生所需的类型,例如
import cats.Id
type IdPerson = Person[Id]
type OptPerson = Person[Option]
在哪里cats.Id简单地定义为type Id[A] = A。编写自己的代码很简单,但是我建议使用cats的代码,因为它附带了有用的typeclass实例。
使用Shapeless可以定义类型类
import shapeless.ops.{hlist, product, tuple}
import shapeless.poly.~>
import shapeless.{Generic, HList, Id, the}
trait Partial[A] {
  type Out
}
object Partial {
  type Aux[A, Out0] = Partial[A] { type Out = Out0 }
  object optionPoly extends (Id ~> Option) {
    override def apply[T](t: T): Option[T] = null
  }
//    implicit def mkPartial[A, L <: HList, L1 <: HList](implicit
//      generic: Generic.Aux[A, L],
//      mapper: hlist.Mapper.Aux[optionPoly.type, L, L1],
//      tupler: hlist.Tupler[L1]): Aux[A, tupler.Out] = null
  implicit def mkPartial[A, T](implicit
    toTuple: product.ToTuple.Aux[A, T],
    mapper: tuple.Mapper[T, optionPoly.type],
    ): Aux[A, mapper.Out] = null
}
并使用它(the是的改进版本implicitly)
case class Person(name: String, age: Int)
// val pp = the[Partial[Person]]
// type PersonPartial = pp.Out
type PersonPartial = the.`Partial[Person]`.Out
implicitly[PersonPartial =:= (Option[String], Option[Int])]