我有两个案例类,我们称它们case class User
为 & case class Ticket
。这两个案例类都实现了成为相同成员所需的操作TypeClass
,在本例中是 Argonaut 的EncodeJson
。
是否可以在不创建它们都扩展的空标记类型的情况下将这两种不同的类型视为相同?
trait Marker
case class User extends Marker
case class Ticket extends Marker
为了具体化,我们有两个单独的函数返回这些案例类:
case class GetUser(userId: Long) extends Service[Doesn't Matter, User] {
def apply(req: Doesn't Matter): Future[User] = {
magical and awesome business logic
return Future[User]
}
}
case class GetTicket(ticketId: Long) extends Service[Doesn't Matter, Ticket] {
def apply(req: Doesn't Matter): Future[Ticket] = {
magical and awesome business logic
return Future[Ticket]
}
}
我想组合这两个服务,以便它们返回相同的类型,在这种情况下argonaut.Json
,但编译器对隐式转换的响应是“LOLNO”
implicit def anyToJson[A](a: A)(implicit e: EncodeJson[A]): Json = e(a)
有任何想法吗?谢谢!