amnaredo / test

0 stars 0 forks source link

Return types on Readers and Writers #180

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

The following code compiles and runs. However, comment out the :Writer[SortOrder] at line A and it fails with the error "Couldn't derive type pkl.SortOrder.SortOrder" on the calls to read and write in main. Restore that return type and comment out ":Reader[SortOrder]" at line B and it fails with "diverging implicit expansion for type upickle.default.Reader[T1]".

If this can be fixed, that would be wonderful. As a short term "fix", would you please update the documentation at http://www.lihaoyi.com/upickle-pprint/upickle/#CustomPicklers to include the return types with a note that sometimes they are necessary? Would have saved me several hours.

Thanks!

package pkl

import upickle.default._
import upickle.Js

object Main {

  import SortOrder._

  def main(args: Array[String]): Unit = {
    val prefs = AdvisorPrefs(SortOrder.Ascending, SortOrder.Descending)

    println(upickle.default.read[AdvisorPrefs](""" {"acad_record_sort_order":"ASC","notes_sort_order":"DESC"} """))
    println(upickle.default.write(prefs))
  }
}

object SortOrder extends Enumeration {
  type SortOrder = Value
  val Ascending, Descending = Value

  implicit val writer:Writer[SortOrder] = upickle.default.Writer[SortOrder] {     // A
    case t ⇒ Js.Str(t.toString)
  }

  implicit val reader:Reader[SortOrder] = upickle.default.Reader[SortOrder] {  // B
    case Js.Str("ASC") ⇒ SortOrder.Ascending
    case Js.Str("DESC") ⇒ SortOrder.Descending
    case x ⇒ throw new Exception("Couldn't interpret $x as a SortOrder value.")
  }
}

case class AdvisorPrefs(
                         acad_record_sort_order: SortOrder.SortOrder = SortOrder.Descending,
                         notes_sort_order: SortOrder.SortOrder = SortOrder.Ascending
                         ) 

ID: 162 Original Author: bwbecker

amnaredo commented 3 years ago

Bug bankruptcy Original Author: lihaoyi