amnaredo / test

0 stars 0 forks source link

Runtime MatchError if a type alias and its ReaderWriter are defined #259

Open amnaredo opened 2 years ago

amnaredo commented 2 years ago

This is not exactly a bug, it is kind of a feature request.

If I create an "alias" of a type and proceed to create a ReadWriter to the alias, the program compiles but fails on with scala.MatchError on runtime while serializing an object of the aliased type.

In my use case it, the problematic type was used in a case class with an default value, so it would only fail in the rare cases where the default value was not used.

Bellow is the simplest reproduction of the problem I could manage to create.

import upickle.default.{ReadWriter, Writer, macroRW, write}
import scala.util.Try

object Example {
  type Problem = List[Int]
  final case class A(x: Problem = List()) extends AnyVal
  implicit val ARw: ReadWriter[A] = macroRW
  implicit val XRw: ReadWriter[Problem] = macroRW // Not needed, and if commented out solves the MatchError.

  def main(args: Array[String]) {

    def serialize[T](data: T)(implicit writer: Writer[T]): String = {
      write(data)
    }

    println(A()) // OK
    println(A(List())) // OK
    println(Try(serialize(A(List(0, 1))))) // scala.MatchError
    println(Try(serialize(List(0, 1).asInstanceOf[Problem]))) // scala.MatchError
  }
}

ID: 289 Original Author: Governa