Scala 使用Shapeless从case类派生新类型

Scala 使用Shapeless从case类派生新类型,scala,shapeless,Scala,Shapeless,假设我有 case class User(id: Long, name: String, age: Long, email: Option[String]) 有没有一种方法我可以创造,比如说 type UpdateUser = ???.???[User] // update user has same fields as User except all are optional 然后我可以用它作为 UpdateUser(name = Some("foo"), email = Some("fo

假设我有

case class User(id: Long, name: String, age: Long, email: Option[String])
有没有一种方法我可以创造,比如说

type UpdateUser = ???.???[User] // update user has same fields as User except all are optional
然后我可以用它作为

UpdateUser(name = Some("foo"), email = Some("foo@bar.com"))
所以基本上是映射类型

Long         :: String         :: Long         :: Option[String] 
->
Option[Long] :: Option[String] :: Option[Long] :: Option[String]
所以再一次,问题是,是否有方法(即使用shapeless)创建具有此类字段的派生case类(无需编写宏来实现这一点)

案例分类只是列表。我们可以基于某一类型的HList创建一个新类型吗?

您不能生成从头开始的具体类型,除非使用宏注释。您可以使用typeclass派生具有所需转换形状的泛型类型

import shapeless._, labelled._

trait RecordAsOption[L] {
  type Out <: HList
}
object RecordAsOption {
  def apply[L](implicit recordAsOption: RecordAsOption[L])
      : Aux[L, recordAsOption.Out] =
    recordAsOption
  type Aux[L, Out0 <: HList] = RecordAsOption[L] { type Out = Out0 }
  implicit def hnilRecordAsOption[L <: HNil]: Aux[L, HNil] =
    new RecordAsOption[L] {
      type Out = HNil
      def apply(l: L) = HNil
    }

  implicit def hconsRecordAsOption[K, V, T <: HList](
      implicit tail: RecordAsOption[T])
      : Aux[FieldType[K, V] :: T, FieldType[K, Option[V]] :: tail.Out] =
    new RecordAsOption[FieldType[K, V] :: T] {
      type Out = FieldType[K, Option[V]] :: tail.Out
    }

  implicit def genericRecordAsOption[T, R](
    implicit lg: LabelledGeneric.Aux[T, R], roa: RecordAsOption[T])
      : Aux[T, roa.Out] =
    new RecordAsOption[T] {
      type Out = roa.Out
    }
}

case class User(id: Long, name: String, age: Long, email: Option[String])
val genericUserUpdate = RecordAsOption[User]
type GenericUserUpdate = genericUserUpdate.Out
import shapeless.\uux,带标签_
性状记录选项[L]{

键入可能重复的…这里的问题是什么?它不是重复的。问题是,如果我们可以,以及如何使用shapeledgeneric派生新的case类,而不显式地编写宏。我认为投票是不合理的。
GenericUser
从何而来?一个错误;我最初是做
LabelledGeneric
part,但后来我把它移到了typeclass中。