【发布时间】:2018-08-20 18:37:03
【问题描述】:
我想将案例类的构造限制为某些类型,然后能够来回编组这些数据。
例如,假设我有一个接受“kind”参数的“home”案例类。我想将“种类”参数限制为已批准的住房类型列表,例如公寓、公寓等。
object Home {
// this will create an implicit conversion to json object
implicit lazy val jsFormat = Jsonx.formatCaseClass[Home]
}
case class Home(owner: String, kind: HousingType)
我现在需要的是一种方法来编组HousingType 的各种子类型。例如,这里有一些关系:
trait HousingType
case object Apartment extends HousingType
case object Condo extends HousingType
可以预见的是,在不指定隐式转换的情况下尝试使用它会产生以下错误:
"could not find implicit value for parameter helper: ... HousingType"
有没有办法为此创建一个通用的隐式转换?
【问题讨论】:
标签: scala types marshalling