Magnolia: Type derivation fails in case of nested type classes

1.3k views Asked by At

I am trying to create a serializable trait that has a dependency on type class.

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]]
}

Here is a reproducible link https://scastie.scala-lang.org/3PnmF52hSkuduzGP10wTdg

But type derivation for this fails with the error magnolia: could not find any direct subtypes of trait Random

I am using zio-schema which internally uses magnolia. I tried adding implicit Derivation of typeclass too but that didn't help too.

0

There are 0 answers