1

我正在尝试创建一个依赖于类型类的可序列化特征。

package dsl
import zio.schema._

sealed trait Random[A] {
  def generate: A
}

object Random {
  case object RandomDouble extends Random[Double] {
    override def generate: Double                           = ???
    implicit val RandomDoubleSchema: Schema[Random[Double]] = DeriveSchema.gen[Random[Double]]
  }
}

sealed trait DummyExpr[A] {
  def eval(value: DummyExpr[A]): A
}

object DummyExpr {
  case object DummyTrue  extends DummyExpr[Boolean] {
    override def eval(value: DummyExpr[Boolean]): Boolean = ???
  }
  case object DummyFalse extends DummyExpr[Boolean] {
    override def eval(value: DummyExpr[Boolean]): Boolean = ???
  }

  case class DummyOperator[A](random: Random[A], predicate: DummyExpr[Boolean]) extends DummyExpr[A] {
    override def eval(value: DummyExpr[A]): A = ???
  }
}

object main extends App {
  val schemaRandom = DeriveSchema.gen[Random[Double]]
  val schemaDummy  = DeriveSchema.gen[DummyExpr[Double]]
}

这是一个可重现的链接https://scastie.scala-lang.org/3PnmF52hSkuduzGP10wTdg

但是对此的类型推导失败并出现错误magnolia: could not find any direct subtypes of trait Random

我正在使用内部使用 magnolia 的 zio-schema。我也尝试添加类型类的隐式推导,但这也无济于事。

4

0 回答 0