Frage

I am attempting to make a generalized serializer for my ADT datatypes which follow the following structured type

  /**
   * Our standard ADT representation, indexed by ID. ADT's also have a formattedString (formal name representing ADT)
   */
  type ADT = {def id: Long; def formattedName:String}
  /**
   * This is a structured type that forces the companion objects associated with an ADT to have an all value. This all
   * value is an enumeration of all of the sum types of the ADT, and is generated by a macro
   * @tparam A
   */
  type ADTCompanion[A] = {val all:Set[A]}

I have also written helper functions in dealing with ADT's, i.e.

  /**
   * Look up an sum type by its id
   * @param companion The companion object being looked up
   * @param id The id to look up
   * @tparam A The type of the companion object
   * @return The corresponding sum type
   */

  def getADT[A <: ADT](companion:ADTCompanion[A], id:Long) = {
    var re:Option[A] = None
    try {
      for (item <- companion.all) {
        if (item.id == id) {
          re = Some(item)
        }
      }
      re.getOrElse(throw new InvalidId(companion,id))
    } catch {
      case e:Throwable => throw new InvalidId(companion,id)
    }
  }

  /**
   * Look up a sum type by its formattedName
   * @param companion The companion object being looked up
   * @param formattedName The formattedName to look up
   * @tparam A The type of the companion object
   * @return The corresponding sum type
   */

  def getADT[A <: ADT](companion:ADTCompanion[A], formattedName:String) = {
    var re:Option[A] = None
    try {
      for (item <- companion.all) {
        if (item.formattedName == formattedName) {
          re = Some(item)
        }
      }
      re.getOrElse(throw new InvalidFormattedName(companion,formattedName))
    } catch {
      case e:Throwable => throw new InvalidFormattedName(companion,formattedName)
    }
  }

Now my issue is in the following

  private def jsonADTSerializer[A <: ADT](adtName:String,adt:ADTCompanion[A]):(PartialFunction[JValue, A], PartialFunction[Any,JValue]) = (
    {
      case JObject
        (JField
          (`adtName`,
            JObject(List(JField("id",JString(id)),JField("formattedName",formattedName)))) :: _) => getADT(adt,id.toLong)
    },
    {
      case x: A => {
        adtName -> (
          ("formattedName" -> x.formattedName) ~
            ("id" -> x.id)
          )
      }
    }
  )

  sealed abstract class Test(val id: Long,val formattedName:String)
  case object Test1 extends Test(1,"Test 1")
  case object Test2 extends Test(2,"Test 2")

  object Test {
    val all:Set[Test] = SealedContents.values[Test]
  }

  sealed abstract class Foo(val id:Long, val formattedName:String)
  case object Foo1 extends Foo(1,"Foo 1")

  object Foo {
    val all:Set[Foo] = SealedContents.values[Foo]
  }

  class TestSerializer extends CustomSerializer[Test](format => jsonADTSerializer("Test",Test))
  class FooSerializer extends CustomSerializer[Foo](format => jsonADTSerializer("Foo",Foo))

As you can see, I am trying to reduce boilerplate by using the jsonADTSerializer function in serializing my ADTs, however I am getting the following warning message when compiling the above code

42: abstract type pattern A is unchecked since it is eliminated by erasure
[warn]       case x: A => {

And unsurprisingly I get java.lang.NoSuchMethodException errors about when using the above code (since the A is erased, the case:x is picking up everything, so I get classes pulled into jsonADTSerializer which don't conform to the A type.)

How do you make sure that A's type inside jsonADTSerializer doesn't get erased so the function works as intended?

Also if you are wondering, I am using the sealed contents macro which you can find here https://stackoverflow.com/a/13672520/1519631

Info on scalatra's json serialization can be found here http://www.scalatra.org/guides/formats/json.html

UPDATE : Changed the typetage of jsonADTSerializer to private def jsonADTSerializer[A <: ADT : ClassTag](adtName:String,adt:ADTCompanion[A]):(PartialFunction[JValue, A], PartialFunction[Any,JValue]) = ( removes the warning message about erasure and fixes the problem, thanks!

War es hilfreich?

Lösung

I think you would just need to add in a ClassTag Context Bound to you method definition:

import scala.reflect._
private def jsonADTSerializer[A <: ADT : ClassTag](adtName:String,adt:ADTCompanion[A]):(PartialFunction[JValue, A], PartialFunction[Any,JValue]) = (

...rest as before...
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top